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
    评论
可以使用 Vue 的 transition 组件来实现表格上下轮播图的效果。具体实现步骤如下: 1. 在表格中添加一个 div 容器,用于包裹需要轮播的图片。 2. 使用 CSS 将这个容器设置为固定高度,同时设置 overflow 属性为 hidden,以便实现上下滚动的效果。 3. 在 Vue 中定义一个变量,用于记录当前显示的图片索引。 4. 使用 setInterval 函数定时切换图片,同时在切换时使用 transition 组件实现过渡效果。 下面是代码示例: ```html <template> <div class="table-container"> <table> <thead> <tr> <th>序号</th> <th>名称</th> <th>图片</th> </tr> </thead> <tbody> <tr v-for="(item, index) in dataList" :key="index"> <td>{{ index + 1 }}</td> <td>{{ item.name }}</td> <td> <div class="img-container"> <transition name="slide-up"> <img :src="item.images[currentIndex]" :key="currentIndex"> </transition> </div> </td> </tr> </tbody> </table> </div> </template> <script> export default { data() { return { dataList: [ { name: '图片1', images: ['image1-1.jpg', 'image1-2.jpg', 'image1-3.jpg'] }, { name: '图片2', images: ['image2-1.jpg', 'image2-2.jpg', 'image2-3.jpg'] }, { name: '图片3', images: ['image3-1.jpg', 'image3-2.jpg', 'image3-3.jpg'] } ], currentIndex: 0 } }, mounted() { // 每 3 秒钟切换一次图片 setInterval(() => { this.currentIndex = (this.currentIndex + 1) % 3 }, 3000) } } </script> <style> .table-container { max-height: 300px; overflow-y: auto; } .img-container { height: 100px; overflow: hidden; } .slide-up-enter-active, .slide-up-leave-active { transition: transform 0.5s; } .slide-up-enter, .slide-up-leave-to { transform: translateY(100%); } </style> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值