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

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

核心特性

  1. 多方向无缝滚动

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

  2. 动态交互控制

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

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

  3. 响应式与动态数据

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

  4. 复杂场景适配

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

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

安装与配置

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

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

    javascript 复制代码
    <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 水平单步滚动停止宽度

使用示例

基础列表滚动
html 复制代码
<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>
表格内容滚动(保留表头)
html 复制代码
<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>
相关推荐
惊鸿一博5 分钟前
java_网络服务相关_gateway_nacos_feign区别联系
java·开发语言·gateway
Bruce_Liuxiaowei9 分钟前
深入理解PHP安全漏洞:文件包含与SSRF攻击全解析
开发语言·网络安全·php
成工小白10 分钟前
【C++ 】智能指针:内存管理的 “自动导航仪”
开发语言·c++·智能指针
sc写算法13 分钟前
基于nlohmann/json 实现 从C++对象转换成JSON数据格式
开发语言·c++·json
Andrew_Xzw18 分钟前
数据结构与算法(快速基础C++版)
开发语言·数据结构·c++·python·深度学习·算法
库库的里昂19 分钟前
【C++从练气到飞升】03---构造函数和析构函数
开发语言·c++
多多*2 小时前
LUA+Reids实现库存秒杀预扣减 记录流水 以及自己的思考
linux·开发语言·redis·python·bootstrap·lua
Dontla3 小时前
为什么React列表项需要key?(React key)(稳定的唯一标识key有助于React虚拟DOM优化重绘大型列表)
javascript·react.js·ecmascript
Wish3D3 小时前
阿里云OSS 上传文件 Python版本
开发语言·python·阿里云
凤年徐3 小时前
【数据结构初阶】单链表
c语言·开发语言·数据结构·c++·经验分享·笔记·链表