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

目录

效果展示

代码

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>
相关推荐
bloglin9999932 分钟前
npm和nvm和nrm有什么区别
前端·npm·node.js
2501_910227541 小时前
web3 前端常见错误类型以及错误捕获处理
前端·web3
哎哟喂_!1 小时前
Node.js 同步加载问题详解:原理、危害与优化策略
前端·chrome·node.js
__BMGT()1 小时前
C++ QT图片查看器
前端·c++·qt
OK_boom2 小时前
React-useRef
javascript·react.js·ecmascript
未来之窗软件服务2 小时前
solidwors插件 开发————仙盟创梦IDE
前端·javascript·数据库·ide·仙盟创梦ide
小白学大数据2 小时前
基于Scrapy-Redis的分布式景点数据爬取与热力图生成
javascript·redis·分布式·scrapy
Varpb2 小时前
【vue】【环境配置】项目无法npm run serve,显示node版本过低
前端·vue.js·npm
读心悦2 小时前
CSS 溢出内容处理、可见性控制与盒类型设置深度解析
前端·css
Minyy113 小时前
Vue3指令(二)--v-text、v-html数据渲染,计算属性
前端·javascript·vue.js·前端框架·vue·html