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


Posted in Javascript onDecember 03, 2018

根据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遇到轮播图不能自动轮播问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
JQuery中$(document)是什么意思有什么作用
Jul 21 Javascript
使用微信内置浏览器点击下拉框出现页面乱跳转现象(iphone),该怎么办
Jan 04 Javascript
一篇文章掌握RequireJS常用知识
Jan 26 Javascript
JavaScript 对象字面量讲解
Jun 06 Javascript
jquery计算出left和top,让一个div水平垂直居中的简单实例
Jul 13 Javascript
微信小程序 wxapp内容组件 icon详细介绍
Oct 31 Javascript
微信小程序 获取相册照片实例详解
Nov 16 Javascript
javascript中href和replace的比较(详解)
Nov 25 Javascript
浅谈javascript alert和confirm的美化
Dec 15 Javascript
js定时器实例分享
Dec 20 Javascript
JavaScript中发出HTTP请求最常用的方法
Jul 12 Javascript
解决vue路由后界面没有变化,但是链接有的问题
Sep 01 Javascript
vue-cli3.0+element-ui上传组件el-upload的使用
Dec 03 #Javascript
利用jquery和BootStrap实现动态滚动条效果
Dec 03 #jQuery
微信小程序实现页面下拉刷新和上拉加载功能详解
Dec 03 #Javascript
微信小程序在地图选择地址并返回经纬度简单示例
Dec 03 #Javascript
vue.js实现的全选与全不选功能示例【基于elementui】
Dec 03 #Javascript
创建Vue项目以及引入Iview的方法示例
Dec 03 #Javascript
利用Vue构造器创建Form组件的通用解决方法
Dec 03 #Javascript
You might like
phpQuery占用内存过多的处理方法
2013/11/13 PHP
php通过文件流方式复制文件的方法
2015/03/13 PHP
List the Stored Procedures in a SQL Server database
2007/06/20 Javascript
javascript实现轮显新闻标题链接
2007/08/13 Javascript
不能再简单的无闪刷新验证码原理很简单
2007/11/05 Javascript
动态加载外部javascript文件的函数代码分享
2011/07/28 Javascript
原生JS实现拖拽图片效果
2020/08/27 Javascript
浅谈如何实现easyui的datebox格式化
2016/06/12 Javascript
简洁实用的BootStrap jQuery手风琴插件
2016/08/31 Javascript
JS实现简单易用的手机端浮动窗口显示效果
2016/09/07 Javascript
js与jquery分别实现tab标签页功能的方法
2016/11/18 Javascript
分类解析jQuery选择器
2016/11/23 Javascript
JavaScript下拉菜单功能实例代码
2017/03/01 Javascript
Vue单页式应用(Hash模式下)实现微信分享的实例
2017/07/21 Javascript
Vue子组件向父组件通信与父组件调用子组件中的方法
2018/06/22 Javascript
Vue3 中的数据侦测的实现
2019/10/09 Javascript
微信小程序缓存支持二次开发封装实现解析
2019/12/16 Javascript
[38:31]完美世界DOTA2联赛PWL S3 Magma vs GXR 第一场 12.13
2020/12/17 DOTA
解决vscode python print 输出窗口中文乱码的问题
2018/12/03 Python
详解python配置虚拟环境
2019/04/08 Python
Python3enumrate和range对比及示例详解
2019/07/13 Python
python 图像处理画一个正弦函数代码实例
2019/09/10 Python
pytorch  网络参数 weight bias 初始化详解
2020/06/24 Python
解决python3输入的坑——input()
2020/12/05 Python
python爬取股票最新数据并用excel绘制树状图的示例
2021/03/01 Python
H5页面适配iPhoneX(就是那么简单)
2019/12/02 HTML / CSS
Weblogic的布署方式
2013/08/23 面试题
研究生自我鉴定范文
2013/10/30 职场文书
机械专业毕业生推荐信范文
2013/11/25 职场文书
五一活动标语
2014/06/30 职场文书
申报优秀教师材料
2014/12/16 职场文书
离婚案件上诉状
2015/05/23 职场文书
2016年寒假见闻
2015/10/10 职场文书
Go语言 go程释放操作(退出/销毁)
2021/04/30 Golang
分析ZooKeeper分布式锁的实现
2021/06/30 Java/Android
python的变量和简单数字类型详解
2021/09/15 Python