vue 使用iView组件中的Table实现定时自动滚动效果

 更新时间:2024年05月25日 12:13:01   作者:小鲤鱼ya  
要在css中设置table的高度,使数据过多时出现滚动条,将纵向设置为overflow-y: auto;横向设置隐藏 overflow-x: hidden,接下来通过本文介绍vue使用iView组件中的Table实现定时自动滚动效果,需要的朋友可以参考下

封装Table

要在css中设置table的高度,使数据过多时出现滚动条,将纵向设置为overflow-y: auto;横向设置隐藏 overflow-x: hidden;

<template>
  <div class="table_container">
    <Table :loading="tableLoading" :columns="columns" :data="dataList" ref="tableL"></Table>
  </div>
</template>
<script>
export default {
  name: "tableList",
  props: {
    columns: {
      type: Array,
      default: () => []
    },
    dataList: {
      type: Array,
      default: () => []
    },
  },
  data () {
    return {
      showContentHeight: 0,
      tableBodyHeight: 0,
      tableLoading: false,
    }
  },
  methods: {
    //动态滚动
    dynamicScroll() {
      let that = this
      this.$nextTick(() => {
        clearInterval(this.timer)
        const table = this.$refs.tableL;
        let tableBody = table.$el.__vue__.$refs.body;
        if (tableBody) {
          let showContentHeight = tableBody.offsetHeight;
          let tableBodyHeight = tableBody.scrollHeight;
          that.showContentHeight = showContentHeight
          that.tableBodyHeight = tableBodyHeight
          if(tableBodyHeight > showContentHeight) {
            that.timerScroll()
          }
        }
      });
    },
    //定时滚动
    timerScroll() {
      let that = this
      const tmpTimer = setInterval(() => {
        const table = that.$refs.tableL;
        let tableBody = table.$el.__vue__.$refs.body;
        if (tableBody) {
          let canScrollHeight = that.tableBodyHeight - that.showContentHeight
          let scrollTop = tableBody.scrollTop
          console.log('scrollTop', scrollTop)
          scrollTop += that.showContentHeight;
          if(scrollTop > canScrollHeight) {
            scrollTop = canScrollHeight
          }
          tableBody.scrollTop = scrollTop;
        }
      }, 5 * 1000);
      this.timer = tmpTimer
      this.$once("hook:beforeDestroy", () => {
        clearInterval(tmpTimer);
      });
    }
  },
}
</script>
<style scoped lang="less">
.table_container {
  height: 100%;
}
.table_container /deep/ .ivu-table-wrapper {
  height: 100%;
  border: none;
  border-bottom: 0;
  border-right: 0;
}
.table_container /deep/ .ivu-table-body {
  height: calc(100% - 40px);	//减掉表头的高度
  overflow-x: hidden;
  overflow-y: auto;
}
.table_container /deep/ .ivu-table-column-center {
  background-color: #39698D;
  color: white;
}
.table_container /deep/ tbody .ivu-table-column-center {
  color: #89D5EA;
}
.table_container /deep/ .ivu-table {
  background-color:rgba(255,255,255, 0);
  color: #89D5EA;
}
.table_container /deep/ .ivu-table td {
  background-color:rgba(255,255,255, 0);
  border-bottom: 1px solid #496893;
}
.table_container /deep/ .ivu-table-tip {
  color: #89D5EA;
}
.table_container /deep/ .ivu-table:before,.table_container /deep/ .ivu-table:after {
  background-color: rgba(255,255,255, 0);
}
.table_container /deep/ .ivu-table th {
  border-bottom: none;
}
/** .ivu-table-body 滚动条样式*/
.table_container /deep/ .ivu-table-body::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 5px; /*高宽分别对应横竖滚动条的尺寸*/
  height: 3px;
}
.table_container /deep/ .ivu-table-body::-webkit-scrollbar-thumb {
  /*滚动条里面小方块*/
  border-radius: 10px;
  height: 20px;
  -webkit-box-shadow: inset 0 0 5px black;
}
.table_container /deep/ .ivu-table-body::-webkit-scrollbar-track {
  /*滚动条里面轨道*/
  -webkit-box-shadow: inset 0 0 5px #6B90B6;
  border-radius: 10px;
  background: #ffffff;
}
</style>

在引用组件的页面调用定时滚动方法

<template>
  <div class="layout">
    <table-list ref="tableList" :columns="columns" :data-list="warehouseList"/>
  </div>
</template>
<script>
import { columns } from './config'
import tableList from "@/components/tableList";
export default {
  name: "board",
  components: {
    tableList,
  },
  data () {
    return {
      columns,
      warehouseList: [],
      resultData: {},
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      getWarehouseList({}).then(res => {
        console.log('getWarehouseList', res)
        if(res.success) {
          this.resultData = res.result
          this.warehouseList = res.result.warehouseList
          const tableList = this.$refs.tableList;
           //动态滚动
          tableList.dynamicScroll()
        }
      })
    }
  }
}
</script>
<style scoped lang="less">
.layout {
  width: 100%;
  height: 100%;
  background:url("../../../assets/prod_board.png") no-repeat center -2px;
  background-size: 100% 100%;
  color: #fff;
}
</style>

到此这篇关于vue 使用iView组件中的Table实现定时自动滚动的文章就介绍到这了,更多相关vue Table定时自动滚动内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 浅析Vue3中useRouter怎么在Vue组件外使用

    浅析Vue3中useRouter怎么在Vue组件外使用

    useRouter 是 Vue 3 Composition API 中的钩子(hook),它只能在 Vue 组件中使用,本文主要来和大家探讨一下如何让他在组件外使用,感兴趣的可以了解下
    2024-11-11
  • 详解vue-cli项目中用json-sever搭建mock服务器

    详解vue-cli项目中用json-sever搭建mock服务器

    这篇文章主要介绍了详解vue-cli项目中用json-sever搭建mock服务器,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • VUE3使用JSON编辑器的详细图文教程

    VUE3使用JSON编辑器的详细图文教程

    最近项目中有用到json编辑器,我选用了这款vue的编辑器,看起来也是比较简洁,接下来就具体介绍一下它,下面这篇文章主要给大家介绍了关于VUE3使用JSON编辑器的详细图文教程,需要的朋友可以参考下
    2023-04-04
  • 分享Vue组件传值的几种常用方式(二)

    分享Vue组件传值的几种常用方式(二)

    这篇文章主要介绍了分享Vue组件传值的几种常用方式,文章围绕主题斩开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • vue打包项目版本号自加的操作步骤

    vue打包项目版本号自加的操作步骤

    项目每次打包后都需要改动项目版本号,这个改动每次都需要在package.json中修改version,比较麻烦,到底有没有一种打包后版本号自加的办法,这篇文章主要介绍了vue打包项目版本号自加的步骤,需要的朋友可以参考下
    2022-09-09
  • vue.js路由跳转详解

    vue.js路由跳转详解

    这篇文章主要为大家详细介绍了vue.js路由跳转的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • 详解Vue前端对axios的封装和使用

    详解Vue前端对axios的封装和使用

    这篇文章主要介绍了Vue前端对axios的封装和使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • vue 结合webpack的初级使用指南小白学习篇

    vue 结合webpack的初级使用指南小白学习篇

    这篇文章主要为大家介绍了vue 结合webpack的初级使用指南非常适合入门webpack的小白学习,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • vue3使用高德地图进行轨迹绘制及播放代码示例

    vue3使用高德地图进行轨迹绘制及播放代码示例

    这篇文章主要介绍了如何定义地图容器及操作按钮,使用高德地图API进行轨迹绘制及播放的方法,并强调了界面样式的重要性,高德地图API的使用需要注册获取key,并且设置了地图容器的大小,需要的朋友可以参考下
    2024-11-11
  • vue中的v-if和v-show的区别详解

    vue中的v-if和v-show的区别详解

    这篇文章主要介绍了vue中的v-if和v-show的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09

最新评论