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定时自动滚动内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue.js iview打包上线后字体图标不显示解决办法

    vue.js iview打包上线后字体图标不显示解决办法

    这篇文章主要介绍了vue.js iview打包上线后字体图标不显示解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • Vue项目配置在局域网下访问方式

    Vue项目配置在局域网下访问方式

    这篇文章主要介绍了Vue项目配置在局域网下访问方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • vue-socket.io跨域问题有效解决方法

    vue-socket.io跨域问题有效解决方法

    在本篇文章里小编给大家整理的是关于vue-socket.io跨域问题有效解决方法,对此有兴趣的朋友们可以参考下。
    2020-02-02
  • Vue3中slot插槽基本使用

    Vue3中slot插槽基本使用

    插槽slot可以说在一个Vue项目里面处处都有它的身影,比如我们使用一些UI组件库的时候,我们通常可以使用插槽来自定义我们的内容,这篇文章主要介绍了Vue3中slot插槽使用方式,需要的朋友可以参考下
    2022-08-08
  • Vue SPA 如何解决浏览器缓存问题

    Vue SPA 如何解决浏览器缓存问题

    这篇文章主要介绍了Vue SPA 如何解决浏览器缓存问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • 解决ant-design-vue中menu菜单无法默认展开的问题

    解决ant-design-vue中menu菜单无法默认展开的问题

    这篇文章主要介绍了解决ant-design-vue中menu菜单无法默认展开的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • 使用Vue纯前端实现发送短信验证码并实现倒计时

    使用Vue纯前端实现发送短信验证码并实现倒计时

    在实际的应用开发中,涉及用户登录验证、密码重置等场景时,通常需要前端实现发送短信验证码的功能,以提升用户体验和安全性,以下是一个简单的前端实现,演示了如何在用户点击发送验证码按钮时触发短信验证码的发送,并开始一个倒计时
    2024-04-04
  • vue中手动封装iconfont组件解析(三种引用方式的封装和使用)

    vue中手动封装iconfont组件解析(三种引用方式的封装和使用)

    这篇文章主要介绍了vue中手动封装iconfont组件(三种引用方式的封装和使用),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • vue使用CSS插件scss时代码报红问题

    vue使用CSS插件scss时代码报红问题

    这篇文章主要介绍了vue使用CSS插件scss时代码报红问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • 微信小程序实战基于vue2实现瀑布流的代码实例

    微信小程序实战基于vue2实现瀑布流的代码实例

    瀑布流,又称瀑布流式布局,是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚动条向下滚动,这种布局还会不断加载数据块并附加至当前尾部,这篇文章主要介绍了微信小程序实战,基于vue2实现瀑布流,需要的朋友可以参考下
    2022-12-12

最新评论