使用better-scroll实现滚动效果,参照大佬的文章,将better-scroll封装成组件可供多次调用。
如何封装slider组件参考文档中解释的非常详细,但有一些细节可以提出。
<div class="slider" ref="slider">//最外层wrapper
<div class="slider-group" ref="sliderGroup">//content层
<slot>
slot>
div>
div>
_setSliderWidth() {
this.children = this.$refs.sliderGroup.children
//width: the width of sliderGroup(content)
//sliderWidth: the width of slider(wrapper)
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) {
width += 2 * sliderWidth
}
this.$refs.sliderGroup.style.width = width + 'px'
}
2.调用slider组件
<slider>
<div v-for="item in recommends">
<a href="item.linkUrl">
<img :src="item.picUrl">
a>
div>
slider>
3.设置样式
由于为横向滚动图片,因此列表中图片摆放应该为从左到右,所以对于slider-item来说应该设置float为left。对于sliderGroup来说应该设置overflow为hidden才能支持正常滚动。
<style scoped lang="stylus" rel="stylesheet/stylus">
.slider
min-height: 1px
.slider-group
overflow: hidden
.slider-item
float: left
img
width: 100%
style>
4.添加当前页面提示
为了直观的表示当前滚动页面所在位置,可以在当滚动页面上添加排列点,当滚动到某一页面时相对应的点数样式发生变化。
//html
<div class="slider" ref="slider">
<div class="slider-group" ref="sliderGroup">
<slot>
</slot>
</div>
<div class="dots">
<span class="dot" v-for="(item, index) in dots" :class="{active: currentPageIndex === index }"></span>
</div>
</div>
//css
.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
其中有关dots的设定中,设置dots位置为absolute,为了使其宽度和屏幕相同,设置left和right都为0,居中显示则设置text-align为center,为了避免莫名其妙的空隙设置font-size为0。
对于每一个点,动态绑定一个active属性,如果滚动到的页面和当前点数相同则改变其样式。
同时,该滑动组件还需要能够自动播放。
//为实现自动横向滑动等功能,可以如下初始化better-scroll
this.slider = new BScroll(this.$refs.slider, {
scrollX: true,
scrollY: false,
momentum: false,
snap: {
loop: this.loop,
threshold: 0.3,
speed: 400
},
click: true
})
记录当前页面索引,每当页面滚动一次,更新当前页面索引,由于在dot中绑定的class会在当前页面索引与点数索引一致时触发,因此可以再组件初始化时绑定一个scrollEnd事件(better-scroll自带),代码如下:
//getCurrentPage为better-scroll提供的函数,但其返回的页面索引从1开始,而点数索引从0开始,因此使用时需要-1
this.slider.on('scrollEnd', () => {
let pageIndex = this.slider.getCurrentPage().pageX
this.currentPageIndex = pageIndex-1
if(this.autoPlay) {
clearTimeout(this.timer)
this._play()
}
})
//跳向下一个页面,当前索引+1为当前页面数,+2为下一页面数
_play() {
let pageIndex = this.currentPageIndex + 2
this.timer = setTimeout(() => {
this.slider.goToPage(pageIndex, 0, 400)
}, this.interval);
}
//slider.vue
<template>
<div class="slider" ref="slider">
<div class="slider-group" ref="sliderGroup">
<slot>
slot>
div>
<div class="dots">
<span class="dot" v-for="(item, index) in dots" :class="{active: currentPageIndex === index }">span>
div>
div>
template>
<script type="text/ecmascript-6">
import {addClass} from 'common/js/dom'
import BScroll from 'better-scroll'
export default {
data() {
return {
dots: [],
currentPageIndex: 0
}
},
props: {
loop: {
type: Boolean,
default: true
},
autoPlay: {
type: Boolean,
default: true
},
interval: {
type: Number,
default: 4000
}
},
mounted() {
setTimeout(() => {
this._setSliderWidth()
this._initDots()
this._initSlider()
}, 20)
window.addEventListener('resize', () => {
if(!this.slider) {
return
}
this._setSliderWidth(true)
this.slider.refresh
})
if(this.autoPlay) {
this._play()
}
},
methods: {
_initDots() {
this.dots = new Array(this.children.length)
},
_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() {
this.slider = new BScroll(this.$refs.slider, {
scrollX: true,
scrollY: false,
momentum: false,
snap: {
loop: this.loop,
threshold: 0.3,
speed: 400
},
click: true
})
this.slider.on('scrollEnd', () => {
let pageIndex = this.slider.getCurrentPage().pageX
// console.log("pageIndex:" + pageIndex)
// console.log("currentPageIndex:" + this.currentPageIndex)
// console.log(this.loop)
if (this.loop) {
pageIndex -= 1
}
// console.log("pageIndex:" + pageIndex);
this.currentPageIndex = pageIndex
if(this.autoPlay) {
clearTimeout(this.timer)
this._play()
}
})
},
_play() {
// console.log("currentPageIndex:" + this.currentPageIndex);
let pageIndex = this.currentPageIndex + 1
// console.log("pageIndex:" + pageIndex)
if (this.loop) {
pageIndex += 1
}
// console.log("pageIndex:" + pageIndex);
this.timer = setTimeout(() => {
this.slider.goToPage(pageIndex, 0, 400)
}, this.interval);
}
},
destroyed() {
clearTimeout(this.timer) //当组件中用到timer时,在销毁时要清除
}
}
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>