vue自动滚动组件 可以支持鼠标滚轮操作
<template>
<div ref="scrollMain" class="auto-scroll-list-main" @click="autoScrollClick($event)"
:key="keyValue" @mouseover="mEnter" @mouseleave="mLeave"
>
<div ref="scrollItemBox" class="seamless-warp-box" >
<slot/>
</div>
<div v-html="copyHtml" class="seamless-warp-box"></div>
</div>
</template>
<script>
import LOG from '@/utils/self-log'
export default {
name: 'AutoScrollList',
props: {
listData: {
type: Array
},
isPause: {
type: Boolean,
default: false
},
keyValue: { type: String, default: '' }
},
mounted() {
this.initScroll()
},
data() {
return {
scrollTop: 0,
speed: 70,
copyHtml: null,
scrollInterval: null,
}
},
watch: {
isPause(newValue, _) {
LOG.info('isPause='+ newValue)
if (newValue) {
this.mEnter()
} else {
this.mLeave()
}
},
listData(newValue, _) {
this.mEnter()
this.initScroll()
},
},
methods: {
initScroll() {
this.$nextTick(() => {
this.copyHtml = this.$refs.scrollItemBox.innerHTML
this.startScroll()
})
},
mEnter() {
if (this.scrollInterval) {
clearInterval(this.scrollInterval)
this.scrollInterval=null
}
},
mLeave() {
this.startScroll()
},
startScroll() {
this.scrollTop=this.$refs.scrollMain.scrollTop
this.scrollInterval = setInterval(this.scroll, this.speed)
},
scroll() {
this.scrollTop=this.scrollTop+1
this.$refs.scrollMain.scrollTop=this.scrollTop
let scrollItemBox = this.$refs.scrollMain.scrollHeight/2
if (this.scrollTop >= scrollItemBox) {
this.scrollTop = 0
}
},
autoScrollClick(e) {
let index = e.target.dataset.index
if (index === undefined) {
return
}
this.$emit('autoScrollClick', index)
}
},
destroyed(){
this.mEnter()
}
}
</script>
<style lang="scss" scoped>
.auto-scroll-list-main {
overflow: auto;
height: 100%;
}
.auto-scroll-list-main::-webkit-scrollbar {
display: none
}
.seamless-warp-box {
width: 100%;
}
</style>
|