vue3 div自动滚动
<div ref="divRef"></div>
const timer = ref()
const divRef = ref()
// 自动滚动
function autoScroll() {
if (timer.value) window.clearInterval(timer.value)
// 设置定时器进行滚动
timer.value = setInterval(() => {
const container = unref(divRef)
container.scrollTop += 1
if (container.scrollHeight - container.clientHeight - container.scrollTop <= 10) {
container.scrollTop = 0
}
}, 100) // 滚动频率,单位为毫秒
}
写法2
const autoScroll = () => {
const table = unref(divRef)
const divData = table.$refs.bodyWrapper.getElementsByClassName('el-scrollbar__wrap')[0]
let tableScroll = true
divData.addEventListener('mouseover', () => {
tableScroll = false
})
divData.addEventListener('mouseout', () => {
tableScroll = true
})
divData.scrollTop = top
if (timer.value) clearInterval(timer.value)
timer.value = setInterval(() => {
if (tableScroll) {
divData.scrollTop += 1
if (divData.clientHeight + divData.scrollTop === divData.scrollHeight) {
divData.scrollTop = 0
}
}
}, 100) // 滚动速度
}