根据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:
}
},
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:
});
// 是否派发滚动到底部事件,用于上拉加载
// 切换到下一张的时候派发的事件
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:
},
showDot: {
type: Boolean,
default: true
},
click: {
type: Boolean,
default: true
},
threshold: {
type: Number,
default: 0.3
},
speed: {
type: Number,
default:
}
},
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遇到轮播图不能自动轮播的更多相关文章

  1. swiper轮播问题之一:轮播图内容为动态数据生成时轮播图无法自动轮播

    本人在用H5做移动端项目中使用Swiper遇到的两个问题,因此加深了对Swiper的掌握,分享出来对刚开始接触Swiper的童鞋们或多或少会有帮助.        首先,new Swiper的初始化最 ...

  2. swiper手滑动轮播图后自动轮播失效解决办法

    设置autoplay:true之后,再设置 autoplay:{disableOnInteraction: false} --------------------------------------- ...

  3. 2017年10月21日 CSS常用样式&鼠标样式 以及 jQuery鼠标事件& jQuery图片轮播& jQuery图片自动轮播代码

    css代码 背景与前景 background-color:#0000; //背景色,样式表优先级高 background-image:url(路径); //设置背景图片 background-atta ...

  4. 用Vue来实现音乐播放器(八):自动轮播图啊

    slider.vue组件的模板部分 <template> <div class="slider" ref="slider"> <d ...

  5. bootstrap轮播组件之“如何关闭自动轮播”

    在一个页面里使用多个bootstrap轮播组件的时候,如果还让所有轮播图都自动轮播的话,整个画面都在动,会给用户一种很不好的体验感受.所以,需要关闭轮播图的自动轮播. 关闭方法:去除如下属性即可: d ...

  6. 自定义完美的ViewPager 真正无限循环的轮播图

    网上80%的思路关于Android轮播图无限循环都是不正确的,不是真正意义上的无限循环, 其思路大多是将ViewPager的getCount方法返回值设置为Integer.MAX_VALUE, 然后呢 ...

  7. vue-cli中轮播图vue-awesome-swiper使用方法

    1 npm 安装 npm install vue-awesome-swiper --save 2在所用的组件中引入 import 'swiper/dist/css/swiper.css' import ...

  8. JavaScript实现动态轮播图效果

    功能描述: 1.鼠标经过 左右侧箭头显示,鼠标离开 箭头隐藏 2.动态添加底部小圆圈并绑定单击事件,并且让小圆圈的点击事件和左右箭头点击事件同步 3.拷贝第一张图片添加到ul最后可以实现动态添加图片 ...

  9. 授人以渔式解析原生JS写轮播图

    需求与分析 需求:循环无缝自动轮播五张图,按左右箭头可以手动切换图片,鼠标点击轮播图下面按钮 1 2 3 4 5会跳转到对应的第1 2 3 4 5张图片.鼠标放到轮播图的图片上时不再自动轮播并且左右箭 ...

随机推荐

  1. linux常用开发命令总结

    linux常用命令 文件操作命令 1. cd 目录名/目录名  切换目录 cd .. 切换到上一级目录  (change dictionary) Ctrl+C强制退出命令行,回到上一级 2.ls    ...

  2. 【IT技术概念】WebAPI与传统的WebService有哪些不同?

    在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下,你有很多的选择来构建一个HTTP Services.我分享一下我对 ...

  3. JQuery 数组按指定长度分组

    JQuery方法 // 将data每3个一组进行分组 var data = ['法国','澳大利亚','智利','新西兰','西班牙','加拿大','阿根廷','美国','0','国产','波多黎各' ...

  4. P5369 [PKUSC2018]最大前缀和

    状态压缩 P5369 题意:求所有排列下的最大前缀和之和 一步转化: 求最大前缀和的前缀由数集S组成的方案数, 统计答案时直接乘上sum(S)即可 考虑最大前缀和的性质: 设最大前缀和为sum[i] ...

  5. 全网最新最简单的 OpenJDK13 代码编译

    目录 开始咯 1.下载源码 2.安装编译需要的依赖 3.安装 jdk 12 4.检查配置 5.开始编译 6.验证是否成功 回顾 个人博客原文:全网最新最简单的 OpenJDK13 代码编译 最近因写文 ...

  6. Python 爬虫入门实战

    1. 前言 首先自我介绍一下,我是一个做 Java 的开发人员,从今年下半年开始,一直在各大技术博客网站发表自己的一些技术文章,差不多有几个月了,之前在 cnblog 博客园加了网站统计代码,看到每天 ...

  7. FFmpeg(七)音频的播放

    一.Open SL ES播放声音流程 简单说明 Open SL ES是android内部的接口,本身可以解码音频,但是我们用FFmpeg,,也可以来录音 . SL引擎:上下文 混音器:两路声音的混合 ...

  8. CentOS7 Redis5.0.5环境搭建

    CentOS7 Redis5.0.5环境搭建 1基本环境配置 CentOS Linux release 7.6.1810 (Core) redis 5.0.5 1.下载解压redis.通过wget在官 ...

  9. 我的Spring Boot学习记录(二):Tomcat Server以及Spring MVC的上下文问题

    Spring Boot版本: 2.0.0.RELEASE 这里需要引入依赖 spring-boot-starter-web 这里有可能有个人的误解,请抱着怀疑态度看. 建议: 感觉自己也会被绕晕,所以 ...

  10. 超详细!! sql server 同步数据库 发布 订阅 跨网段 无公网ip 常见问题

    问题描述 主机1:发布端 阿里云服务器--有公网ip 主机2:订阅端 笔记本--无公网ip 数据量很小,主要是熟悉发布订阅的操作流程. 主机2仅仅作为主机1的本地备份,要求修改云服务器上数据后,能通过 ...