// 外部
<slider>
<div v-for="(item, index) in arrs" :key="index"></div>
</slider>
<template>
<div class="slider" ref="slider">
<div class="slider-group" ref="sliderGroup">
<slot>
// 插槽
</slot>
</div>
// dots点
<div class="dots">
<span
class="dot"
v-for="(item,index) in dots"
:class="{active: currentPageIndex === index}"
:key="index"
></span>
</div>
</div>
</template>
<script type="text/ecmascript-6">
import { addClass } from 'common/js/dom'
import BScroll from 'better-scroll' // 引入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(() => { // 浏览器刷新17ms一次
this._setSliderWidth() // 初始化宽度,否则缩放会不匹配
this._initDots() // 初始化点
this._initSlider() // 初始化Slider
if (this.autoPlay) { // 自动播放
this._play()
}
}, 20)// 浏览器刷新17ms一次
// 重新计算宽度
window.addEventListener('resize', () => {
if (!this.slider) { // slider 还没创建 就返回
return
}
this._setSliderWidth(true) // 窗口isResize传入true
this.slider.refresh()
})
},
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) { // 当无线循环轮播 且不是resize时,要多克隆两个dom来循环切换,width + 2 * sliderWidth
width += 2 * sliderWidth
}
this.$refs.sliderGroup.style.width = width + 'px'
},
_initDots() {
this.dots = new Array(this.children.length) // 原写法
// this.dots = new Array(this.children.length - 2)
},
_initSlider() {
this.slider = new BScroll(this.$refs.slider, {
click: true,// 是否可点击
scrollX: true, // 横向移动
scrollY: false, // 纵向移动
momentum: false,// 惯性
snap: { // 这个配置是为了做 Slide 组件用的
loop: this.loop,
threshold: 0.3, // 可滚动到下一个的阈值
speed: 400 // 切换的过渡时间
}
// 老版本写法,新版去掉(防止小圆点对不上):
// if (this.loop) {
// pageIndex -= 1
// }
})
this.slider.on('scrollEnd', () => { // 监听滚动到尽头时
let pageIndex = this.slider.getCurrentPage().pageX
// 老版本有,新版去掉:
// if (this.loop) {
// pageIndex -= 1
// }
this.currentPageIndex = pageIndex
if (this.autoPlay) {
clearTimeout(this.timer)
this._play()
}
})
},
_play () {
let pageIndex = this.currentPageIndex
// 老版本有,新版去掉:
if (this.loop) {
pageIndex += 1
}
pageIndex = pageIndex === this.$refs.sliderGroup.children.length ? 0 : pageIndex
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>
总结
1)mounted初始化,setTimeout 20ms
2)改变视口时,要重新计算宽度,在刷新组件