页面调用
methods:{
toRoll() {
this.animationLiveHood = animationUseScroll(
this.$refs['listWrap'],
{
height: 80,
delay: 3000
}
)
},
}
destoryed() {
this.animationLiveHood = null
}
公用方法
export function animationUseScroll(element, options) {
class Scroll {
constructor(element, options) {
const defaultOption = {
height: 90,
delay: 4000
}
this.options = Object.assign(defaultOption, options)
this.element = element
this.interval = null
this.toRoll()
this.mouseIn()
this.mouseOut()
}
toRoll() {
const divData = this.element
this.interval = setInterval(() => {
if (divData.clientHeight + divData.scrollTop >= divData.scrollHeight) {
divData.scrollTop = 0
} else {
const subInterval = setInterval(() => {
if (divData.scrollTop % this.options.height === 0 || divData.clientHeight + divData.scrollTop >= divData.scrollHeight) {
clearInterval(subInterval)
}
divData.scrollTop += 1
}, 5)
}
}, this.options.delay)
}
mouseIn() {
this.element.addEventListener('mouseenter', () => {
clearInterval(this.interval)
}, false)
}
mouseOut() {
this.element.addEventListener('mouseleave', () => {
this.toRoll()
}, false)
}
}
return new Scroll(element, options)
}
|