for example, setting loop does not take effect when initializing
the demo component of the official website is used
component code
<template>
<!-- -->
<div class="slide" ref="slide">
<div class="slide-group" ref="slideGroup">
<slot>
</slot>
</div>
</div>
</template>
<script type="text/ecmascript-6">
import BScroll from "better-scroll"
const COMPONENT_NAME = "slide"
export default {
name: COMPONENT_NAME,
props: {
dataInfo: {
type: Array,
},
loop: {
type: Boolean,
default: true
},
autoPlay: {
type: Boolean,
default: false
},
interval: {
type: Number,
default: 4000
},
click: {
type: Boolean,
default: true
},
},
data() {
return {
currentPageIndex: 0
}
},
watch: {
dataInfo () {
// scroll
setTimeout(() => {
this.refresh()
}, 60)
}
},
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().pageY
this.slide.goToPage(0, pageIndex, 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._setSlideHeight(true)
},
next() {
this.slide.next()
},
init() {
clearTimeout(this.timer)
this.currentPageIndex = 0
this._initSlide()
this._setSlideHeight()
if (this.autoPlay) {
this._play()
}
},
_setSlideHeight(isResize) {
this.children = this.$refs.slideGroup.children
let height = 0
let slideHeight = this.$refs.slide.clientHeight
for (let i = 0; i < this.children.length; iPP) {
let child = this.children[i]
child.style.height = slideHeight + "px"
height += slideHeight
}
if (this.loop && !isResize) {
height += 2 * slideHeight
}
this.$refs.slideGroup.style.height = height + "px"
},
_initSlide() {
this.slide = new BScroll(this.$refs.slide, {
scrollX: false,
scrollY: true,
momentum: false,
stopPropagation: true,
snap: {
loop: false,
threshold: 0.1,
speed: 400,
easing: {
style: "cubic-bezier(0.275, 0.885, 0.32, 1.275)"
}
},
bounce: false,
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().pageY
this.currentPageIndex = pageIndex
if (this.autoPlay) {
this._play()
}
},
_play() {
clearTimeout(this.timer)
this.timer = setTimeout(() => {
this.slide.next()
}, this.interval)
}
}
}
</script>
<style lang="less" scoped>
.slide {
min-height: 1px;
height: 100vh;
overflow: hidden;
.slide-group {
position: relative;
}
}
</style>
use components
<slide :dataInfo="infoData.SectionList" ref="$Slide">
<template v-if="infoData.Cover_url">
<component :is="infoData.Invitation_Style_Code" :BasicInfo="BasicInfo"
:enrollCount="infoData.EnrollCount" v-on:linkto="linkto"
:backgroundUrl="infoData.Cover_url" :isCostom="infoData.Custom_App_Template"></component>
</template>
</slide>
in addition, if the modified data does not refresh the page after initialization, a cloned element will be produced but the data will not be cloned.