vue-music 使用better-scroll遇到轮播图不能自动轮播问题

根据vue-music视频中slider组建的使用,当安装新版本的better-scroll,轮播组件,不能正常轮播

这是因为,better-scroll发布新版本之后,参数设置发生改变

这是旧版本: 组件为slider

<template>
 <div class="slider" ref="slider">
  <div class="slider-group" ref="sliderGroup">
   <slot>
   </slot>
  </div>
  <div class="dots">
   <span class="dot" :class="{active: currentPageIndex === index }" v-for="(item, index) in dots" :key="item.id"></span>
  </div>
 </div>
</template>
<script type="text/ecmascript-6">
import { addClass } from "common/js/dom";
import BScroll from "better-scroll";
export default {
 name: "slider",
 props: {
  loop: {
   type: Boolean,
   default: true
  },
  autoPlay: {
   type: Boolean,
   default: true
  },
  interval: {
   type: Number,
   default: 4000
  }
 },
 data() {
  return {
   dots: [],
   currentPageIndex: 0
  };
 },
 mounted() {
  setTimeout(() => {
   this._setSliderWidth();
   this._initDots();
   this._initSlider();
   if (this.autoPlay) {
    this._play();
   }
  }, 20);
  window.addEventListener("resize", () => {
   if (!this.slider) {
    return;
   }
   this._setSliderWidth(true);
   this.slider.refresh();
  });
 },
 activated() {
  if (this.autoPlay) {
   this._play();
  }
 },
 deactivated() {
  clearTimeout(this.timer);
 },
 beforeDestroy() {
  clearTimeout(this.timer);
 },
 methods: {
  _setSliderWidth(isResize) {
   this.children = this.$refs.sliderGroup.children;
   let width = 0;
   let sliderWidth = this.$refs.slider.clientWidth;
   for (let i = 0; i < this.children.length; i++) {
    let child = this.children[i];
    addClass(child, "slider-item");
    child.style.width = sliderWidth + "px";
    width += sliderWidth;
   }
   if (this.loop && !isResize) {
    width += 2 * sliderWidth;
   }
   this.$refs.sliderGroup.style.width = width + "px";
  },
  _initSlider() {
   // better-scroll 对外暴露了一个 BScroll 的类
   // Vue.js 提供了我们一个获取 DOM 对象的接口—— vm.$refs
   this.slider = new BScroll(this.$refs.slider, {
    scrollX: true,
    scrollY: false,
    momentum: false,
    snap: true,
    snapLoop: this.loop,
    snapThreshold: 0.3,
    snapSpeed: 400
   });
   // 是否派发滚动到底部事件,用于上拉加载
  // 切换到下一张的时候派发的事件
   this.slider.on("scrollEnd", () => {
    let pageIndex = this.slider.getCurrentPage().pageX;
    if (this.loop) {
     pageIndex -= 1;
    }
    this.currentPageIndex = pageIndex;
    if (this.autoPlay) {
     this._play();
    }
   });
   // 是否派发列表滚动开始的事件
   this.slider.on("beforeScrollStart", () => {
    if (this.autoPlay) {
     clearTimeout(this.timer);
    }
   });
  },
  _initDots() {
   this.dots = new Array(this.children.length);
  },
  _play() {
   let pageIndex = this.currentPageIndex + 1;
   if (this.loop) {
    pageIndex += 1;
   }
   this.timer = setTimeout(() => {
    this.slider.goToPage(pageIndex, 0, 400);
   }, this.interval);
  }
 }
};
</script>
<style scoped lang="stylus" rel="stylesheet/stylus">
@import '~common/stylus/variable';
.slider {
 min-height: 1px;
 .slider-group {
  position: relative;
  overflow: hidden;
  white-space: nowrap;
  .slider-item {
   float: left;
   box-sizing: border-box;
   overflow: hidden;
   text-align: center;
   a {
    display: block;
    width: 100%;
    overflow: hidden;
    text-decoration: none;
   }
   img {
    display: block;
    width: 100%;
   }
  }
 }
 .dots {
  position: absolute;
  right: 0;
  left: 0;
  bottom: 12px;
  text-align: center;
  font-size: 0;
  .dot {
   display: inline-block;
   margin: 0 4px;
   width: 8px;
   height: 8px;
   border-radius: 50%;
   background: $color-text-l;
   &.active {
    width: 20px;
    border-radius: 5px;
    background: $color-text-ll;
   }
  }
 }
}
</style>

下面是版本升级之后,做出的变化

<template>
  <div class="slide" ref="slide">
    <div class="slide-group" ref="slideGroup">
      <slot>
      </slot>
    </div>
    <div v-if="showDot" class="dots">
      <span class="dot" :class="{active: currentPageIndex === index }" v-for="(item, index) in dots" :key="index"></span>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import { addClass } from "common/js/dom";
import BScroll from "better-scroll";
// const COMPONENT_NAME = "slide";
export default {
//  name: COMPONENT_NAME,
 props: {
  loop: {
   type: Boolean,
   default: true
  },
  autoPlay: {
   type: Boolean,
   default: true
  },
  interval: {
   type: Number,
   default: 4000
  },
  showDot: {
   type: Boolean,
   default: true
  },
  click: {
   type: Boolean,
   default: true
  },
  threshold: {
   type: Number,
   default: 0.3
  },
  speed: {
   type: Number,
   default: 400
  }
 },
 data() {
  return {
   dots: [],
   currentPageIndex: 0
  };
 },
 mounted() {
  this.update();
  window.addEventListener("resize", () => {
   if (!this.slide || !this.slide.enabled) {
    return;
   }
   clearTimeout(this.resizeTimer);
   this.resizeTimer = setTimeout(() => {
    if (this.slide.isInTransition) {
     this._onScrollEnd();
    } else {
     if (this.autoPlay) {
      this._play();
     }
    }
    this.refresh();
   }, 60);
  });
 },
 activated() {
  if (!this.slide) {
   return;
  }
  this.slide.enable();
  let pageIndex = this.slide.getCurrentPage().pageX;
  this.slide.goToPage(pageIndex, 0, 0);
  this.currentPageIndex = pageIndex;
  if (this.autoPlay) {
   this._play();
  }
 },
 deactivated() {
  this.slide.disable();
  clearTimeout(this.timer);
 },
 beforeDestroy() {
  this.slide.disable();
  clearTimeout(this.timer);
 },
 methods: {
  update() {
   if (this.slide) {
    this.slide.destroy();
   }
   this.$nextTick(() => {
    this.init();
   });
  },
  refresh() {
   this._setSlideWidth(true);
   this.slide.refresh();
  },
  prev() {
   this.slide.prev();
  },
  next() {
   this.slide.next();
  },
  init() {
   clearTimeout(this.timer);
   this.currentPageIndex = 0;
   this._setSlideWidth();
   if (this.showDot) {
    this._initDots();
   }
   this._initSlide();
   if (this.autoPlay) {
    this._play();
   }
  },
  _setSlideWidth(isResize) {
   this.children = this.$refs.slideGroup.children;
   let width = 0;
   let slideWidth = this.$refs.slide.clientWidth;
   for (let i = 0; i < this.children.length; i++) {
    let child = this.children[i];
    addClass(child, "slide-item");
    child.style.width = slideWidth + "px";
    width += slideWidth;
   }
   if (this.loop && !isResize) {
    width += 2 * slideWidth;
   }
   this.$refs.slideGroup.style.width = width + "px";
  },
  _initSlide() {
   console.log(this.threshold);
   this.slide = new BScroll(this.$refs.slide, {
    scrollX: true,
    scrollY: false,
    momentum: false,
    snap: {
     loop: this.loop,
     threshold: this.threshold,
     speed: this.speed
    },
    bounce: false,
    stopPropagation: true,
    click: this.click
   });

  this.slide.on("scrollEnd", this._onScrollEnd);
   this.slide.on("touchEnd", () => {
    if (this.autoPlay) {
     this._play();
    }
   });
   this.slide.on("beforeScrollStart", () => {
    if (this.autoPlay) {
     clearTimeout(this.timer);
    }
   });
  },
  _onScrollEnd() {
   let pageIndex = this.slide.getCurrentPage().pageX;
   this.currentPageIndex = pageIndex;
   if (this.autoPlay) {
    this._play();
   }
  },
  _initDots() {
   this.dots = new Array(this.children.length);
  },
  _play() {
   clearTimeout(this.timer);
   this.timer = setTimeout(() => {
    this.slide.next();
   }, this.interval);
  }
 },
 watch: {
  loop() {
   this.update();
  },
  autoPlay() {
   this.update();
  },
  speed() {
   this.update();
  },
  threshold() {
   this.update();
  }
 }
};
</script>

<style lang="stylus" rel="stylesheet/stylus">
@import '../../common/stylus/variable';

.slide {
  min-height: 1px;

  .slide-group {
    position: relative;
    overflow: hidden;
    white-space: nowrap;

    .slide-item {
      float: left;
      box-sizing: border-box;
      overflow: hidden;
      text-align: center;

      a {
        display: block;
        width: 100%;
        overflow: hidden;
        text-decoration: none;
      }

      img {
        display: block;
        width: 100%;
      }
    }
  }

  .dots {
    position: absolute;
    right: 0;
    left: 0;
    bottom: 12px;
    transform: translateZ(1px);
    text-align: center;
    font-size: 0;

    .dot {
      display: inline-block;
      margin: 0 4px;
      width: 8px;
      height: 8px;
      border-radius: 50%;
      background: $color-text-l;

      &.active {
        width: 20px;
        border-radius: 5px;
        background: $color-text-ll;
      }
    }
  }
}
</style>

可参考官方文档:https://github.com/ustbhuangyi/better-scroll/blob/master/example/components/slide/slide.vue

总结

以上所述是小编给大家介绍的vue-music 使用better-scroll遇到轮播图不能自动轮播问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • vue利用better-scroll实现轮播图与页面滚动详解

    前言 better-scroll 也很强大,不仅可以做普通的滚动列表,还可以做轮播图.picker 等等...所以本文主要给大家介绍了关于vue用better-scroll实现轮播图与页面滚动的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 1.安装better-scroll 在根目录中package.json的dependencies中添加: "better-scroll": "^0.1.15" 然后 npm i 安装. 2.封装代码

  • Vue中如何实现轮播图的示例代码

    这个功能我感觉在任何项目中都会涉及到,今天我就把我的实现方法跟大家分享一下,有不对的地方还请指出,我好更新. 下面是整体代码,我把轮播图单独做了一个组件,大家可以拿来就用,具体代码如下: <template> <div class="content"> <div class="focus"> <!-- focus begin --> <swiper :options="swiperOption"

  • 基于vue.js轮播组件vue-awesome-swiper实现轮播图

    一般做移动端轮播图的时候,最常用的就是Swiper插件了,而vue.js也有一个轮播组件vue-awesome-swiper,用法跟swiper相似. 1.安装vie-awesome-swiper nam install vue-awesome-swiper --save-dev 2.引用vie-awesome-swiper组件,这里我是用vie-cli创建的项目,在main.js: import VueAwesomeSwiper from 'vue-awesome-swiper'; Vue.u

  • vue-music关于Player播放器组件详解

    本文实例为大家分享了关于Player播放器组件的具体内容,供大家参考,具体内容如下 迷你播放器: 1.播放器组件会在各个页面的情况下会打开. 首先在vuex state.js 中定义全局的播放器状态 import {playMode} from 'common/js/config.js'; const state = { singer:{}, playing:false, //是否播放 fullScreen:false, //是否全屏 playList:[], //播放列表 sequenceLi

  • vue使用 better-scroll的参数和方法详解

    格式:var obj = new BScroll(object,{[option1,],.,.}); 注意: 1.要确保object元素的高度比其父元素高 2.使用时,一定要确保object所在的dom渲染后,再用上面的语句,或者obj.refresh() Options 参数 startX: 0 开始的X轴位置 startY: 0 开始的Y轴位置 scrollY: true 滚动方向为 Y 轴 scrollX: true 滚动方向为 X 轴 click: true 是否派发click事件,通常

  • Vue 过渡实现轮播图效果

    Vue 过渡 Vue 的过渡系统是内置的,在元素从 DOM 中插入或移除时自动应用过渡效果. 过渡的实现要在目标元素上使用 transition 属性,具体实现参考Vue2 过渡 下面例子中我们用到列表过渡,可以先学习一下官方的例子 要同时渲染整个列表,比如使用 v-for,我们需要用到 <transition-group> 组件 Vue 轮播图 我们先看这样一个列表 <ul> <li v-for="list in slideList"> <i

  • 详解 vue better-scroll滚动插件排坑

    BetterScroll号称目前最好用的移动端滚动插件,因此它的强大之处肯定是存在的.要不...哈哈.个人感觉还是很好用的.这篇文章不是笼统的讲 BetterScroll ,而是单讲滚动,想要深入了解它,请移步这里. 滚动原理 better-scroll 是什么滚动原理 better-scroll 是一款重点解决移动端(已支持 PC)各种滚动场景需求的插件.它的核心是借鉴的 iscroll 的实现,它的 API 设计基本兼容 iscroll,在 iscroll 的基础上又扩展了一些 featur

  • vue-music 使用better-scroll遇到轮播图不能自动轮播问题

    根据vue-music视频中slider组建的使用,当安装新版本的better-scroll,轮播组件,不能正常轮播 这是因为,better-scroll发布新版本之后,参数设置发生改变 这是旧版本: 组件为slider <template> <div class="slider" ref="slider"> <div class="slider-group" ref="sliderGroup"&

  • Vue+ECharts实现中国地图的绘制及各省份自动轮播高亮显示

    目录 实现效果 完整代码+详细注释 要点小结 实现效果 完整代码+详细注释 <template> <div class="echart"> <div class="content"> <div id="map_cn"></div> </div> </div> </template> <script> import echarts from

  • vue仿携程轮播图效果(滑动轮播,下方高度自适应)

    先看案例,使用vue+swiper实现,slide不同高度时,动态计算盒子高度,让其下方高度自适应的效果 首先搭建vue项目,这里不做过多说明,然后安装swiper npm install swiper --save-dev 1. js部分:初始化swiper组件,vue要在mounted生命周期中进行初始化,代码如下: import Swiper from 'swiper' import { TweenMax, Power2 } from 'gsap' 初始化时调用resize函数,计算屏幕容

  • 基于JavaScript实现轮播图原理及示例

    网上有很多的例子介绍,在这里我所做的无缝滚动就是 通过改变元素的left值让图片呈现左右滚动的效果. 我们首先看一下 div+css 的结构样式: div+css代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> *{ margin: 0; pa

  • js实现轮播图的完整代码

    今天写一个完整的轮播图,首先它需要实现三个功能: 1.鼠标放在小圆点上实现轮播 2.点击焦点按钮实现轮播 3.无缝自动轮播 轮播图的原理: 一系列的大小相等的图片平铺,利用CSS布局只显示一张图片,其余隐藏.通过计算偏移量(封装一个动画函数)自动播放,或通过手动点击事件切换图片. html布局: <div id="box" class="all"> <div class="inner"> <!-- 相框-->

  • java制作广告图片自动轮播控件

    首页图片的轮播 /** * 广告图片自动轮播控件</br> * */ public class ImageCycleView extends LinearLayout { /** * 上下文 */ private Context mContext; /** * 图片轮播视图 */ private ViewPager mAdvPager = null; /** * 滚动图片视图适配 */ private ImageCycleAdapter mAdvAdapter; /** * 图片轮播指示器控件

  • vue轮播图插件vue-awesome-swiper的使用代码实例

    最近写vue2.0项目中用到了轮播图的一个插件,也就是vue-awesome-swiper,个人感觉还是比较强大的,swiper官网中的API及配置均可使用(支持3.0),以下说下使用该插件的一些步骤: 第一步安装 npm install vue-awesome-swiper --save 第二部在main.js中引入 import Vue from 'vue' import VueAwesomeSwiper from 'vue-awesome-swiper' Vue.use(VueAwesom

  • vue.js实现简单轮播图效果

    学习了vue.js也有一段时间了,做了个小demo来熟悉一下,很常见的demo,-------轮播图,没学vue之前的轮播图用JavaScript或者jquery都非常简单,发现用vue来写也挺有意思的.说下简单的思路,图片的轮播用v-if或者v-show来代替原来的Js滑动,过度效果用transition可简单实现,注意,滑动过程中是能看见两张图的,所以要用两个transition. (1)先写出整体的框架 <template> <div class="slide-show&

随机推荐