【记录】列表自动滚动轮播功能实现

目录

效果展示

代码

html 复制代码
<!-- 首页 -->
<template>
  <div class="page_body_item_body" @mouseenter="stopScroll" @mouseleave="scroll(false)">
    <ele-table
      class="eleTable"
      :table-options="options"
      :columns-options="columns"
      :show-fixed-control="false"
      :show-pagination="false"
    />
  </div>
</template>

<script>
import { pageVoiceAlertByJh } from '@/api/video-api.js'

export default {
  name: 'Home',
  components: {},

  data() {
    return {
      tableData: [],
      tableMaxHeight: 400,
      tableScrollTimer: null
    }
  },

  computed: {
    options() {
      return {
        data: this.tableData || [],
        maxHeight: this.tableMaxHeight
      }
    },
    columns() {
      return [
        {
          type: 'index',
          label: '序号'
        },
        {
          prop: 'xm',
          label: '警员姓名',
          'show-overflow-tooltip': true
        },
        {
          prop: 'jh',
          label: '警号',
          'show-overflow-tooltip': true
        },
        {
          prop: 'bmmc',
          label: '所在单位',
          'show-overflow-tooltip': true
        },
        {
          prop: 'zfycCount',
          label: '执法异常',
          'show-overflow-tooltip': true
        },
        {
          prop: 'sgycCount',
          label: '事故处理异常',
          'show-overflow-tooltip': true,
          minWidth: 100
        },
        {
          prop: 'sumCount',
          label: '总异常',
          'show-overflow-tooltip': true
        }
      ]
    }
  },

  created() {
    this.pageVoiceAlertByJh()
  },

  mounted() {
    this.$nextTick(() => {
      this.tableMaxHeight = this.getTableHeight()
    })
    window.addEventListener('resize', () => {
      this.tableMaxHeight = this.getTableHeight()
    })
  },

  methods: {
    pageVoiceAlertByJh() {
      pageVoiceAlertByJh({
        kssj: '2024-01-01',
        jssj: '2024-12-01'
      }).then((res) => {
        this.$common.CheckCode(res, null, () => {
          this.tableData = res.data?.rows || []

          this.$nextTick(() => {
            this.scroll(true)
          })
        })
      })
    },

	// 列表停止滚动
    stopScroll() {
      clearInterval(this.tableScrollTimer)
      this.tableScrollTimer = null
    },
	
	// 列表开始滚动
    scroll(isFetchData = false) {
      this.stopScroll()

      const body_content = document.querySelector('.el-table__body-wrapper')
      const body_content_heigh = body_content?.offsetHeight
      const body = document.querySelector('.el-table__body')
      const body_heigh = body?.offsetHeight
      isFetchData && (body_content.scrollTop = 0)

	  // 判断列表的高度是否高于列表父盒子的高度
      if (body_heigh > body_content_heigh) {
        const cha = body_heigh - body_content_heigh
        this.tableScrollTimer = setInterval(() => {
          if (body_content.scrollTop >= cha) {
            body_content.scrollTop = 0
          } else body_content.scrollTop += 1
        }, 50)
      }
    },

    // 获取 table 最大高度
    getTableHeight() {
      const tableContainer = document.querySelector('.table')
      const tableHeader = document.querySelector('.table page_body_item_header')
      const tableContainerHeight = (tableContainer && tableContainer.offsetHeight) || 0
      const tableHeaderHeight = (tableHeader && tableHeader.offsetHeight) || 0
      // return tableContainerHeight - tableHeaderHeight - 50 - 34
      return tableContainerHeight - tableHeaderHeight - 50 - 2
    }
  }
}
</script>

<style lang='scss' scoped>
</style>
相关推荐
ᥬ 小月亮5 分钟前
webpack基础
前端·webpack
YongGit24 分钟前
探索 AI + MCP 渲染前端 UI
前端·后端·node.js
慧一居士1 小时前
<script setup>中的setup作用以及和不带的区别对比
前端
RainbowSea1 小时前
NVM 切换 Node 版本工具的超详细安装说明
java·前端
读书点滴2 小时前
笨方法学python -练习14
java·前端·python
Mintopia2 小时前
四叉树:二维空间的 “智能分区管理员”
前端·javascript·计算机图形学
慌糖2 小时前
RabbitMQ:消息队列的轻量级王者
开发语言·javascript·ecmascript
Mintopia2 小时前
Three.js 深度冲突:当像素在 Z 轴上玩起 "挤地铁" 游戏
前端·javascript·three.js
Penk是个码农2 小时前
web前端面试-- MVC、MVP、MVVM 架构模式对比
前端·面试·mvc
markyankee1012 小时前
Vue.js 入门指南:从零开始构建你的第一个应用
vue.js