vue 页面内实现el-table和div自动滚动

vue 页面内实现el-table和div自动滚动

html 复制代码
<template>
    <div>
        <div>el-table自动滚动</div>
        <el-table
                  ref="tableListWrapper"
                  :data="tableData"
                  height="315"
                  >
            <el-table-column type="index" label="序号" width="40" align="center"></el-table-column>
            <el-table-column prop="deviceName" label="名称" show-overflow-tooltip align="center"></el-table-column>
            <el-table-column prop="status" label="状态" show-overflow-tooltip align="center"></el-table-column>
        </el-table>

        <div>普通div自动滚动</div>
        <div class="divBox" ref="divListWrapper" @mouseenter="handleListMouseEnter" @mouseleave="handleListMouseLeave">
            <div v-for="(item, index) in list2" :key="index" class="item"> {{item.name}}</div>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                tableData: [],
                list2: [],
                scrollInterval: null,
                scrollTableInterval: null,
            }
        },
        mounted() {
            this.startAutoScroll()
            this.startTableAutoScroll()
        },
        beforeDestroy() {
            this.stopAutoScroll()
            this.stopTableAutoScroll()
        },
        methods: {
            handlemapmouseenter() {
                this.stopAutoScroll()
                this.stopTableAutoScroll()
            },
            handlemapmouseleave() {
                this.startAutoScroll()
                this.startTableAutoScroll()
            },
            handleListMouseEnter() {
                this.stopAutoScroll()
            },
            handleListMouseLeave() {
                this.startAutoScroll()
            },
            startAutoScroll() {
                if (this.scrollInterval) {
                    return
                }
                this.scrollInterval = setInterval(() => {
                    if (this.$refs.divListWrapper) {
                        const wrapper = this.$refs.divListWrapper
                        if (wrapper) {
                            wrapper.scrollTop += 1
                            if (wrapper.scrollTop + wrapper.clientHeight >= wrapper.scrollHeight) {
                                wrapper.scrollTop = 0
                            }
                        }
                    }
                }, 50)
            },
            // 停止自动滚动
            stopAutoScroll() {
                if (this.scrollInterval) {
                    clearInterval(this.scrollInterval)
                    this.scrollInterval = null
                }
            },
            ////////////////////
            startTableAutoScroll() {
                if (this.scrollTableInterval) {
                    return
                }
                const scrollWrap = this.$refs.tableListWrapper.$el.querySelector('.el-table__body-wrapper')

                scrollWrap.addEventListener('mouseenter', this.stopTableAutoScroll)
                scrollWrap.addEventListener('mouseleave', this.startTableAutoScroll)

                this.scrollTableInterval = setInterval(() => {
                    if (scrollWrap) {
                        const wrapper = scrollWrap
                        if (wrapper) {
                            wrapper.scrollTop += 1
                            if (wrapper.scrollTop + wrapper.clientHeight >= wrapper.scrollHeight) {
                                wrapper.scrollTop = 0
                            }
                        }
                    }
                }, 50)
            },
            // 停止自动滚动
            stopTableAutoScroll() {
                if (this.scrollTableInterval) {
                    clearInterval(this.scrollTableInterval)
                    this.scrollTableInterval = null
                }
            },
        },
    }
</script>
相关推荐
科研邪修法3 小时前
PDF新增页面文字与文本批注怎么区分?以双版本培训讲义为例
前端·css·pdf
武昌库里写JAVA3 小时前
SpringBoot+Vue 项目介绍
java·javascript·git·sql·ai
Lanwarf-前端开发4 小时前
Vue3 + Canvas/WebGL 雷达扫描可视化实战:PPI、RHI、VOL、SVOL 四种模式原理与实现
前端·javascript·vue.js·webgl
FRing035 小时前
vue3干货总结2(vue3生命周期、父子通信、模版引用、provide和inject)
开发语言·前端·vue.js·vue3
锋行天下7 小时前
langgraph 全栈项目(三)
前端·人工智能·后端
YIAN7 小时前
从 LangChain 到 DeepAgents:手把手拆解 createAgent、Middleware 与复杂 Agent
javascript·langchain·deepseek
锋行天下7 小时前
langgraph 全栈项目(二)
前端·人工智能·后端
冬夜Zis8 小时前
顺着作用域链和 outer 指针,一次性看懂闭包底层逻辑
javascript
Ceelog8 小时前
Mac 上怎么找出并安全清理旧的 node_modules?(实测 304 个占 66.3 GB)
javascript