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>
相关推荐
jay神16 分钟前
【计算机毕业设计】基于Flask+Vue的电影数据可视化与智能推荐系统
前端·vue.js·python·信息可视化·flask·毕业设计·课程设计
FreeTinker6 小时前
HTML本地存储技术解析:localStorage与sessionStorage的原理、差异与应用场景
前端·html
qq_452396236 小时前
第十二篇:《全链路监控与可观测性:前端错误追踪与性能分析》
前端
海兰7 小时前
【应用】基于 Next.js 16 + Python mplfinance的金融K线图与技术指标可视化平台(三)
javascript·python·金融
wangruofeng7 小时前
Phosphor Icons 官网源码拆解:URL 即存储、水波动画与 7362 Star 图标库的架构实践
前端·架构·github
BigTopOne8 小时前
WebRTC Native / Android 开发学习资源整理
前端
excel9 小时前
nuxt 3 升级 nuxt 4 报错之 hasInjectionContext
前端
小磊哥er9 小时前
深入解构Claude Code - 第 7 篇 · 连接外面的世界
javascript·ai编程
何以解忧,唯有..9 小时前
LangChain 工具调用(Tool Calling)实战指南
java·前端·langchain
小磊哥er9 小时前
深入解构Claude Code - 第 6 篇 · 终端里的图形界面
javascript·ai编程