前言

目标

1 掌握vue3-seamless-scroll使用方法 2 vue2是否可以使用vue3-seamless-scroll插件


基本使用

前期工作

创建一个vue3项目,在项目中安装第三方组件 1 安装

npm install vue3-seamless-scroll --save

2 引入并注册

import { Vue3SeamlessScroll } from "vue3-seamless-scroll";
import { defineComponent,ref } from "vue";
export default defineComponent({
  name: "SeamlessScroll",
  components: {Vue3SeamlessScroll}   
});

3 使用组件

<vue3-seamless-scroll>
 // 要滚动的内容
</vue3-seamless-scroll>

在正式开始之前,先看一下这个第三方组件有哪些参数或者属性 官方文档:vue3-seamless-scroll 组件配置

key 描述 默认值 类型
step 滚动速度 1 Number
limitMoveNum 开始无缝滚动的最小数据量 5 Number
hover 是否开启鼠标悬停stop true Boolean
direction 方向:up,down,left,right up String
wheel 在开启鼠标悬停的情况下是否开启滚轮滚动 false Boolean
copyNum 拷贝列表次数 1 Number

以上仅列出了接下来可能用到的属性。

表格列滚动效果实现

这里用的是el-table表格组件。 若直接将el-table表格插入到vue-seamless-scroll组件中,会导致el-table表格中的表头也会进行滚动,这不是想要的效果。 这里用两个el-table,一个保留表头部分,一个保留内容部分。保留内容部分的放入vue-seamless-scroll组件中进行无缝滚动。

     <!-- 第一个表格 保留头部 -->
      <el-table class="table" :data="tableData">
        <el-table-column prop="name" label="姓名" width="180"></el-table-column>
        <el-table-column prop="age" label="年龄" width="180"></el-table-column>
        <el-table-column prop="address" label="地址"></el-table-column>
      </el-table>
      <vue3-seamless-scroll
        v-model="isPause"
        class="scroll"
        :list="tableData"
        :limitScrollNum="scrollOptions.limitScrollNum"
        :step="scrollOptions.step"
        :hover="scrollOptions.hover"
        :copyNum="scrollOptions.copyNum"
        :wheel="scrollOptions.wheel"
        :direction="scrollOptions.direction"
      >
        <el-table class="scroll-content" :data="tableData">
          <el-table-column
            prop="name"
            label="姓名"
            width="180"
          ></el-table-column>
          <el-table-column
            prop="date"
            label="日期"
            width="180"
          ></el-table-column>
          <el-table-column prop="address" label="地址"></el-table-column>
        </el-table>
      </vue3-seamless-scroll>

样式

.table .el-table__body-wrapper {
  display: none;
}
.scroll-content .el-table__header {
  display: none;
}
.scroll {
  height: 280px;
  overflow: hidden;
}
.scroll-content {
  overflow: hidden;
}

vue3-seamless-scroll插件中给了一个wheel配置,当为true的时候可以开启滚轮控制。 最终的效果: image.png

vue2项目中如何使用vue3-seamless-scroll

vue2项目可以使用vue3提供的一些第三方组件,但是前提是这些vue3的第三方组件本身支持vue2,否则vue2项目无法使用。 vue3-seamless-scroll是用vue3开发的,目前仅仅支持vue3的项目使用,不支持vue2项目使用。