<template>
<div class="home">
<HelloWorld msg="Welcome to Your Vue.js App"/>
<div class="slide" ref="slide">
<ul ref="ul">
<li>
<img src="../assets/slide-1.jpg" alt="">
</li>
<li>
<img src="../assets/slide-2.jpg" alt="">
</li>
<li>
<img src="../assets/slide-3.jpg" alt="">
</li>
</ul>
<div class="dot">
<span v-for="(item,index) in dots" :class="{active: currentIndex === index}"></span>
</div>
</div>
<div ref="wrapper" class="list-wrapper">
<ul class="list-content">
<li v-for="item in data">{{item}}</li>
</ul>
</div>
</div>
</template>
<script>
import HelloWorld from '@/components/HelloWorld.vue'
import BScroll from 'better-scroll'
export default {
name: 'home',
created () {
for (let i = 0; i < 100; i++) {
this.data[i] = i;
}
},
mounted () {
setTimeout(() => {
this.setSlideWidth();
this.initSlide();
this.play();
},20)
},
data () {
return {
data: [],
scroll: null,
timer: '',
dots: [1,2,3],
currentIndex: 0,
}
},
components: {
HelloWorld
},
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 () {
let options = {
scrollX: true,
scrollY: false,
snap: {
loop: true,
threshold: 0.3,
speed: 400,
}
}
this.slide = new BScroll(this.$refs.slide,options);
this.slide.on('scrollEnd', () => {
this.currentIndex = this.slide.getCurrentPage().pageX;
clearTimeout(this.timer);
this.play();
})
this.slide.on('beforeScrollStart', () => {
clearTimeout(this.timer);
})
},
// 滚动到下一页
play(){
clearTimeout(this.timer);
this.timer = setTimeout(() => {
this.slide.next();
},2000)
}
}
}
</script>
<style scoped lang="less">
.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;
}
}
</style>
为什么点击轮播的图片,轮播就会暂停
在手机上点一下屏幕,轮播也会暂停
如果你想使用
better-scroll
封装slide
等组件建议你先查看demoTips:
better-scroll
推出了2.x demo 是基于1.x的,2.x版本中各种组件上层使用更简洁,你应该使用的是1.x版本