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>
相关推荐
前端老石人2 小时前
HTML文档元素与元数据详解
前端·html
wing982 小时前
用 AI 实现图片懒加载,这也太简单了!
前端·vue.js·图片资源
sigernet2 小时前
Claude Code 不再推荐 npm 安装:官方改为 Native Installer
前端·npm·node.js
lxh01132 小时前
函数防抖题解
前端·javascript·算法
颜酱2 小时前
环检测与拓扑排序:BFS/DFS双实现
javascript·后端·算法
我发现一个问题2 小时前
node+ts+koa全栈框架学习-1
前端
sure2822 小时前
React Native中自定义TabBar
前端·react native·react.js
bluceli2 小时前
CSS自定义属性与主题切换:构建动态UI的终极方案
前端·css
默默学前端2 小时前
HTML 高频面试题 5 道|吃透基础,面试不慌(附详细解析)
前端·面试·职场和发展·html5