使用vue3-seamless-scroll实现列表自动滚动播放

发布于:2025-05-11 ⋅ 阅读:(22) ⋅ 点赞:(0)

vue3-seamless-scroll组件支持上下左右无缝滚动,单步滚动,并且支持复杂图标的无缝滚动。

核心特性

  1. 多方向无缝滚动
    支持上下、左右四个方向的自动滚动,通过 direction 参数控制(默认 up),适用于新闻轮播、数据大屏等场景。

  2. 动态交互控制

    • 通过 v-model 绑定布尔值控制滚动启停。

    • 支持鼠标悬停暂停(hover: true)和滚轮手动滚动(wheel: true)。

  3. 响应式与动态数据
    可监听数据变化(isWatch: true),当列表长度达到 limitScrollNum(默认5)时触发滚动,适合动态加载内容。

  4. 复杂场景适配

    • 表格滚动:通过拆分表头与内容区域,避免表头跟随滚动(需复制一个隐藏表头的表格包裹组件)。

    • 单步滚动:通过 singleHeight 或 singleWidth 设置单步停止距离,结合 singleWaitTime 控制停留时间

安装与配置

1. 安装方式
npm install vue3-seamless-scroll --save
# 或
yarn add vue3-seamless-scroll
2. 组件注册
  • 全局注册(推荐):

    // main.js
    import { createApp } from 'vue';
    import vue3SeamlessScroll from "vue3-seamless-scroll";
    const app = createApp(App);
    app.use(vue3SeamlessScroll);
  • 局部注册

    <script setup>
    import { Vue3SeamlessScroll } from "vue3-seamless-scroll";
    </script>
3. 关键配置参数
参数 类型 说明
list Array 必填,滚动数据列表
direction String 滚动方向:up/down/left/right(默认 up
step Number 步进速度,值越大滚动越快
copyNum Number 列表拷贝次数(默认1),用于无缝衔接
hover Boolean 是否启用鼠标悬停暂停(默认 false
singleHeight Number 垂直单步滚动停止高度(设为0则连续滚动)
singleWidth Number 水平单步滚动停止宽度

使用示例

基础列表滚动
<template>
  <vue3-seamless-scroll 
    :list="list" 
    direction="up" 
    :hover="true"
    class="scroll-container"
  >
    <div v-for="(item, index) in list" :key="index" class="item">
      {{ item.title }}
    </div>
  </vue3-seamless-scroll>
</template>

<style>
.scroll-container {
  height: 300px;
  overflow: hidden;
}
.item {
  padding: 12px 0;
}
</style>
表格内容滚动(保留表头)
<template>
  <div class="scroll-wrap">
  <div class="scroll-header">
    <ul class="scroll-ul">
      <li class="scroll-li">
        <span style="min-width: 80px; width: calc(100% - 180px)">标题</span>
        <span style="width: 180px">日期</span>
      </li>
    </ul>
  </div>
  <div class="scroll-content">
    <vue3-seamless-scroll class="scroll-list" :list="list" :hover="true" :step="0.4" :wheel="true" :isWatch="true">
      <ul class="scroll-ul" v-for="(item, index) in list" :key="index">
        <li class="scroll-li">
          <span style="min-width: 80px; width: calc(100% - 180px)">{{ item.title }}</span>
          <span style="width: 180px">{{ item.date }}</span>
        </li>
      </ul>
    </vue3-seamless-scroll>
  </div>
</div>
</template>
 
<script lang="ts" setup>
import { ref } from 'vue';
import { Vue3SeamlessScroll } from "vue3-seamless-scroll";
 
const list = ref([
  {
    title: "Vue3.0 无缝滚动组件展示数据第1条",
    date: Date.now(),
  },
  {
    title: "Vue3.0 无缝滚动组件展示数据第2条",
    date: Date.now(),
  },
  {
    title: "Vue3.0 无缝滚动组件展示数据第3条",
    date: Date.now(),
  },
  {
    title: "Vue3.0 无缝滚动组件展示数据第4条",
    date: Date.now(),
  },
  {
    title: "Vue3.0 无缝滚动组件展示数据第5条",
    date: Date.now(),
  },
  {
    title: "Vue3.0 无缝滚动组件展示数据第6条",
    date: Date.now(),
  },
  {
    title: "Vue3.0 无缝滚动组件展示数据第7条",
    date: Date.now(),
  },
  {
    title: "Vue3.0 无缝滚动组件展示数据第8条",
    date: Date.now(),
  },
  {
    title: "Vue3.0 无缝滚动组件展示数据第9条",
    date: Date.now(),
  },
]);   
</script>
 
<style scoped>
.scroll-wrap {
  width: 500px;
  height: 350px;
  overflow: hidden;
}
.scroll-header,
.scroll-content {
  width: 100%;
	display: flex;
}
.scroll-list {
  width: 100%;
  overflow: hidden;
}
.scroll-ul {
  width: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-li {
  width: 100%;
  display: flex;
  line-height: 35px;
}
.scroll-li > span {
  display: flex;
  height: 35px;
  line-height: 35px;
  border-top: 1px solid #dcdfe6;
  border-left: 1px solid #dcdfe6;
  padding-left: 5px;
  overflow: hidden;
}
.scroll-li > span:last-child {
  border-right: 1px solid #dcdfe6;
}
.scroll-header .scroll-li {
  background-color: #F8F9FF;
}
.scroll-header .scroll-li > span {
  font-weight: bold; 
  border-top: none;
}
.scroll-content .scroll-ul:last-child .scroll-li {
  border-bottom: 1px solid #dcdfe6;
}
</style>


网站公告

今日签到

点亮在社区的每一天
去签到