el-table实现自动滚动;列表自动滚动

el-table的实现

javascript 复制代码
<template>
  <el-table
    v-loading="scrollLoading"
    :data="cameraList"
    ref="scrollTableOne"
    :header-cell-style="{ background: '#f1ff', color: 'red' }"
    :cell-style="{ background: '#fff', color: '#333333' }"
    style="height: 240px"
  >
    <template #empty>
      <!-- 空数据组件 -->
      <jg-empty :type="'noData'" />
    </template>
    <el-table-column prop="messageContent" label="消息描述" show-overflow-tooltip></el-table-column>
    <el-table-column prop="messageType" label="消息类型" show-overflow-tooltip>
      <template #default="scope">{{ scope.row?.messageType === "MT_NOTICE" ? "提醒类" : "操作类" }}</template>
    </el-table-column>
    <el-table-column width="170" prop="sendTime" label="消息时间" show-overflow-tooltip></el-table-column>
  </el-table>
</template>

<script setup>
import { ref, onMounted } from "vue"

let scrollLoading = ref(false);
let cameraList = ref([]);
const tableNode = ref(null);
let scrollTableOne = ref("");
// 函数
const getTableList = () => {
  cameraList.value = Array.from({ length: 10 }).map((ele, index) => {
    return {
      messageContent: '调度机构' + index,
      messageType: index,
      sendTime: + new Date()
    }
  })

  if (cameraList.value.length) {
    tableNode.value =
      scrollTableOne.value.$refs.bodyWrapper.getElementsByClassName(
        "el-scrollbar__wrap"
      )[0];
    const tableScroll = ref(true);
    tableNode.value.addEventListener("mouseover", () => {
      tableScroll.value = false;
    });
    tableNode.value.addEventListener("mouseout", () => {
      tableScroll.value = true;
    });
    setInterval(() => {
      if (tableScroll.value) {
        tableNode.value.scrollTop++;
        if (tableNode.value.clientHeight + tableNode.value.scrollTop === tableNode.value.scrollHeight) {
          tableNode.value.scrollTop = 0;
        }
      }
    }, 30);
  }

}
onMounted(() => {
  getTableList()
})
</script>

正常div通过v-for循环也可以实现

javascript 复制代码
<template>
  <div style="width: 400px">
    <div class="flex-between">
      <div>消息描述</div>
      <div>消息类型</div>
      <div>消息时间</div>
    </div>
    <div style="height: 240px; overflow-y: auto" ref="div_el">
      <div class="flex-between" style="height: 40px" v-for="(item, index) in cameraList" :key="index">
        <div>{{ item.messageContent }}</div>
        <div>{{ item.messageType }}</div>
        <div>{{ item.sendTime }}</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue"

let cameraList = ref([]);
let div_el = ref("");
// 函数
const getTableList = () => {
  cameraList.value = Array.from({ length: 10 }).map((ele, index) => {
    return {
      messageContent: '调度机构' + index,
      messageType: index,
      sendTime: + new Date()
    }
  })

  if (cameraList.value.length) {
    const tableScroll = ref(true);
    div_el.value.addEventListener("mouseover", () => {
      tableScroll.value = false;
    });
    div_el.value.addEventListener("mouseout", () => {
      tableScroll.value = true;
    });
    setInterval(() => {
      if (tableScroll.value && div_el.value) {
        div_el.value.scrollTop++;
        if (div_el.value.clientHeight + div_el.value.scrollTop === div_el.value.scrollHeight) {
          div_el.value.scrollTop = 0;
        }
      }
    }, 30);
  }

}
onMounted(() => {
  getTableList()
})
</script>
<style scoped lang="scss">
.flex-between {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
相关推荐
白帽攻防录3 小时前
SRC 挖洞:PostScript 打印机驱动双 RCE,打印服务器怎么变成内网跳板
开发语言·网络安全·php·内网安全
十年Java程序媛3 小时前
Java枚举Enum完整教程|替代静态常量,开发与面试要点
java·开发语言·spring boot·后端·面试
2603_965148114 小时前
电商选品调研:通过关键词搜索API获取市场热销商品,结合商品详情API分析竞品卖点与定价
开发语言·数据库·人工智能·python·信息可视化
xixiaoyunya5 小时前
Python 异步编程入门:用 asyncio 轻松实现并发任务
开发语言·python·php
学逆向的5 小时前
C 循环结构的执行效率分析
c语言·开发语言·数据结构·网络安全
小坏讲微服务5 小时前
刚发布一个月的JDK 27,为什么我建议你立刻升级?
java·开发语言·spring boot·spring·架构·求职招聘·springboot4
打工仔折腾 AI7 小时前
用 Dashlet 把极空间 NAS 服务入口整理成统一导航首页的实践
java·开发语言·人工智能·后端·python·rpc·ai agent 实战
MATLAB代码顾问7 小时前
Simulink PI 参数扫描与批量仿真完整 MATLAB 代码
开发语言·matlab
Want5957 小时前
Python跳动的爱心②
开发语言·python·数学建模
小羊没烦恼!8 小时前
T-SQL查询进阶--理解SQL SERVER中的分区表
服务器·开发语言·前端·javascript·html