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>
相关推荐
骑着蜗牛撵大象3273 小时前
多 Agent 分治协作:MapReduce 模式下的并行扇出与结果归并
前端
用户55318297323 小时前
Flutter iOS 热更新深入 Dart VM:读懂函数入口、解释执行与调用桥接
前端
前端snow3 小时前
为什么大厂要用Postgres + mogodb的框架?
前端
雪芽蓝域zzs3 小时前
第 3 节:地图点击事件,点击获取经纬度
vue.js
运维有小邓13 小时前
2026年主流AD域管理软件有哪些?
前端
Mahut3 小时前
我在 Mac 上做了个本地剪辑器
javascript·架构·产品
Mickey同学3 小时前
提示词注入:AI 时代的「SQL 注入」
前端
deli0073 小时前
GROUP BY 先别想当然:我把 SQL 分组语义做成了沙盘,8 个实验 + 27 条自检全绿
前端
WayneX3 小时前
开源 Vue 3 组件库 Morya UI:把组件、文档、AI 工具链一起做进一个包
前端·vue.js·前端框架
用户15741568165343 小时前
页面白屏 + Invalid prop: type check failed for prop "options"?原来是 script setup 的 ref
前端