简单修改了下你的代码,点击不会暂停了
import BScroll from 'better-scroll'
export default {
name: 'home',
created() {
for (let i = 0; i < 100; i++) {
this.data[i] = i
}
},
mounted() {
this.$nextTick(() => {
this.setSlideWidth()
this.initSlide()
this._play()
})
},
data() {
return {
data: [],
scroll: null,
timer: null,
dots: [1, 2, 3],
currentIndex: 0
}
},
methods: {
// 设置容器宽度
setSlideWidth() {
let slideWidth = 0
let children = this.$refs.ul.children
for (let i = 0; i < children.length; i++) {
children[i].style.width = this.$refs.slide.clientWidth + 'px'
slideWidth += children[i].offsetWidth
}
this.$refs.ul.style.width = slideWidth + this.$refs.slide.clientWidth * 2 + 'px'
},
// 初始化slide
initSlide() {
this.slide = new BScroll(this.$refs.slide, {
scrollX: true,
scrollY: false,
momentum: false,
snap: {
loop: this.loop || true,
threshold: this.threshold || 0.3,
speed: this.speed || 400
},
bounce: false,
stopPropagation: true,
click: this.click || true
})
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)
// }
})
},
// 滚动到下一页
_play() {
clearTimeout(this.timer)
this.timer = setTimeout(() => {
this.slide.next()
}, 400)
},
_onScrollEnd() {
let pageIndex = this.slide.getCurrentPage().pageX
this.currentIndex = pageIndex
// if (this.autoPlay) {
this._play()
// }
}
}
}
.slide {
width: 100%;
// max-width: 6.4rem;
overflow: hidden;
position: relative;
ul {
list-style-type: none;
position: relative;
overflow: hidden;
li {
float: left;
img {
width: 100%;
}
}
}
.dot {
position: absolute;
text-align: center;
width: 100%;
top: 80%;
span {
display: inline-block;
width: 4px;
height: 4px;
background-color: #fff;
border-radius: 100%;
margin: 0 6px 0 0;
}
span.active {
background-color: red;
width: 14px;
border-radius: 4px;
}
}
}
.list-wrapper {
position: relative;
max-height: 3.5rem;
overflow: hidden;
li {
height: 0.5rem;
line-height: 0.5rem;
}
}
如果你想使用better-scroll封装slide等组件建议你先查看demo
Tips:better-scroll推出了2.x demo 是基于1.x的,2.x版本中各种组件上层使用更简洁,你应该使用的是1.x版本