vue3-seamless-scroll实现表格自动轮播滚动

1,651 阅读3分钟

最近准备做一个自动滚动的表格特效,现已完美实现该功能,所以记录下这个特效的实现过程 vue-seamless-scroll开始用的这个插件,结果按照用法操作,最后会一直报错,显示 vue-seamless-scroll Cannot read properties of undefined (reading '_c'),后来查了下,发现,原来这是vue2适用的插件,用在vue3上就会报错

image.png

接下来,vue3的插件登场了 vue3-seamless-scroll Vue3.0 无缝滚动组件,支持Vite2.0,支持服务端打包

先来看看最终实现的效果

动画.gif

安装

npm install vue3-seamless-scroll --save

组件配置

  • list

    无缝滚动列表数据,组件内部使用列表长度。

      type: Array
      required: true
    
  • v-model

    通过v-model控制动画滚动与停止,默认开始滚动

      type: Boolean,
      default: true,
      required: false
    
  • direction

    控制滚动方向,可选值updownleftright

      type: String,
      default: "up",
      required: false
    
  • isWatch

    开启数据更新监听

      type: Boolean,
      default: true,
      required: false
    
  • hover

    是否开启鼠标悬停

      type: Boolean,
      default: false,
      required: false
    
  • count

    动画循环次数,默认无限循环

      type: Number,
      default: "infinite",
      required: false
    
  • limitScrollNum

    开启滚动的数据量,只有列表长度大于等于该值才会滚动

      type: Number,
      default: 5,
      required: false
    
  • step

    步进速度

      type: Number,
      required: false
    
  • singleHeight

    单步运动停止的高度

      type: Number,
      default: 0,
      required: false
    
  • singleWidth

    单步运动停止的宽度

      type: Number,
      default: 0,
      required: false
    
  • singleWaitTime

    单步停止等待时间(默认值 1000ms)

      type: Number,
      default: 1000,
      required: false
    
  • isRemUnit

    singleHeight and singleWidth 是否开启 rem 度量

      type: Boolean,
      default: true,
      required: false
    
  • delay

    动画延时时间

      type: Number,
      default: 0,
      required: false
    
  • ease

    动画效果,可以传入贝塞尔曲线数值

      type: String | cubic-bezier,
      default: "ease-in",
      required: false
    
  • copyNum

    拷贝列表次数,默认拷贝一次,当父级高度大于列表渲染高度的两倍时可以通过该参数控制拷贝列表次数达到无缝滚动效果

      type: Number,
      default: 1,
      required: false
    
  • wheel

    在开启鼠标悬停的情况下是否开启滚轮滚动,默认不开启

      type: boolean,
      default: false,
      required: false
    
  • singleLine

    启用单行横向滚动

      type: boolean,
      default: false,
    

注意项

需要滚动的列表所在容器必须设置样式 overflow: hidden;

使用

注册组件

  • 全局组件注册 install
  // **main.js**
  import { createApp } from 'vue';
  import App from './App.vue';
  import vue3SeamlessScroll from "vue3-seamless-scroll";
  const app = createApp(App);
  app.use(vue3SeamlessScroll);
  app.mount('#app');
  • 单个.vue文件局部注册
<script>
  import { defineComponent } from "vue";
  import { Vue3SeamlessScroll } from "vue3-seamless-scroll";
   export default defineComponent({
      components: {
        Vue3SeamlessScroll
      }
   })
</script>

使用组件

<template>
  <vue3-seamless-scroll :list="list" class="scroll">
    <div class="item" v-for="(item, index) in list" :key="index">
      <span>{{item.title}}</span>
      <span>{{item.date}}</span>
    </div>
  </vue3-seamless-scroll>
</template>
<script>
import { defineComponent, ref } from "vue";
import { Vue3SeamlessScroll } from "vue3-seamless-scroll";

<style>
.scroll {
  height: 270px;
  width: 500px;
  margin: 100px auto;
  overflow: hidden;
}

.scroll .item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 3px 0;
}
</style>

以上是对列表展示的一些使用,但是如果是表格的使用,会稍微复杂一点,因为用这个插件直接包裹组件的话,表格的表头也会跟着一起滚动,所以为了避免表头滚动,我们搞两个一模一样的表格,上面一个表格只展示表头,隐藏表格的内容,下面一个表格隐藏表头,只展示表格的内容,vue3-seamless-scroll直接包裹我们下面那个表格的body,就可以实现只有表格内容滚动,代码如下

    <div class="data-table">
        <table border="0" cellspacing="0" cellpadding="0">
          <thead>
            <tr class="head">
              <th width="15%">序号</th>
              <th width="20%">紧缆位置</th>
              <th width="30%">紧缆手机</th>
              <th width="20%">孔隙率</th>
              <th width="15%">不圆度</th>
            </tr>
          </thead>

          <tbody style="display:none">
            <tr v-for="(item, index) in tableData" :key="index" :class="index%2?'double':'single'">
              <td width="15%">{{index+1}}</td>
              <td width="20%">{{item.address}}</td>
              <td width="30%">{{item.date}}</td>
              <td width="20%">{{item.porosity}}</td>
              <td width="15%">{{item.notCircle}}</td>
            </tr>
          </tbody>
        </table>
        <vue3-seamless-scroll
          class="seamless"
          :list="tableData"
          :hover="true"
          :step="0.4"
          :wheel="true"
          :isWatch="true"
        >
          <table border="0" cellspacing="0" cellpadding="0">
            <thead style="display:none">
              <tr class="head">
                <th width="15%">序号</th>
                <th width="20%">紧缆位置</th>
                <th width="30%">紧缆手机</th>
                <th width="20%">孔隙率</th>
                <th width="15%">不圆度</th>
              </tr>
            </thead>

            <tbody class="body-animation">
              <tr
                v-for="(item, index) in tableData"
                :key="index"
                :class="index%2?'double':'single'"
              >
                <td width="15%">{{index+1}}</td>
                <td width="20%">{{item.address}}</td>
                <td width="30%">{{item.date}}</td>
                <td width="20%">{{item.porosity}}</td>
                <td width="15%">{{item.notCircle}}</td>
              </tr>
            </tbody>
          </table>
        </vue3-seamless-scroll>
      </div>

上面的代码因为一些表格样式问题,直接使用的原生table写,直接原生写的话,可以上面一个表只写head,下面表格写body就可以了