vue表格自动轮播小组件(简陋)

<template>
  <div class="scorll-table">
    <table class="table" @mouseenter="enter" @mouseleave="start">
      <thead class="thead">
        <tr class="thead-tr">
          <td v-for="(item,index) in config" :key="index" class="thead-tr-td">{{item.title}}</td>
        </tr>
      </thead>
      <tbody class="tbody" ref="tbody anim" :class="{anim:animate==true}">
        <tr v-for="(item,index) in Data" :key="index" class="tbody-tr">
          <td v-for="(res,i) in config" :key="i" class="tbody-tr-td">{{item[res.key]}}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  name: "scorllTable",
  props: {
    //   宽
    width: {
      type: String || Number,
    },
    /**
     * 表头信息
     *  举例:[
     *   {key: "module_name",title: "模块名称"},
     *   {key: "name", title: "成果名称"},
     *   {key: "num",title: "数量"},],
     *   title 表头名字   key 循环表格数据时的key 值
     */
    config: {
      type: Array,
    },
    // 表格数据
    tableData: {
      type: Array,
    },
  },
  data() {
    return {
      Data: [], // 表格数据
      animate: false, // 动画
      time: null, // 定时器
    };
  },
  mounted() {
    this.Data = JSON.parse(JSON.stringify(this.tableData));
    this.time = setInterval(this.openTheScroll, 1000);
  },
  methods: {
    /**
     * @description 开始滚动
     * @param
     */
    openTheScroll() {
      this.animate = true; // 因为在消息向上滚动的时候需要添加css3过渡动画,所以这里需要设置true
      setTimeout(() => {
        //  这里直接使用了es6的箭头函数,省去了处理this指向偏移问题,代码也比之前简化了很多
        this.Data.push(this.Data[0]); // 将数组的第一个元素添加到数组的
        this.Data.shift(); //删除数组的第一个元素
        this.animate = false; // margin-top 为0 的时候取消过渡动画,实现无缝滚动
      }, 500);
    },
    /**
     * @description 鼠标移入
     */
    enter() {
      clearInterval(this.time);
      //   this.Data =JSON.parse(JSON.stringify(this.tableData));
    },
    /**
     * @description 鼠标移除
     */
    start() {
      this.time = setInterval(this.openTheScroll, 1000);
    },
    /**
     * @description 清除定时器
     * @param
     */
    clearTime() {
      clearInterval(this.time);
    },
  },
};
</script>

<style lang="scss" scoped>
.scorll-table {
  width: 100%;
  height: 300px;
  background: #ccc;
  overflow: hidden;
}
.table {
  width: 100%;
  height: 300px;
  display: flex;
  flex-direction: column;
  position: relative;
}
.thead {
  height: 40px;
  line-height: 40px;
  position: absolute;
  width: 100%;
  z-index: 99;
  background: #fff;
}
.thead-tr {
  display: flex;
  .thead-tr-td {
    flex: 1;
  }
}
.tbody {
  flex: 1;
  border: 0.5px solid #fff;
  position: absolute;
  top: 40px;
  left: 0;
  right: -17px;
  bottom: 0;
  overflow-x: hidden;
  overflow-y: scroll;
}
.tbody-tr {
  display: flex;
  .tbody-tr-td {
    flex: 1;
    height: 40px;
    line-height: 40px;
    border: 0.5px solid #fff;
  }
}
.anim {
  transition: all 0.5s;
  margin-top: -40px;
}
</style>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值