vue实现列表上下滚动
实现列表上下滚动的几种方法
使用CSS动画
通过CSS的@keyframes和animation属性实现自动滚动效果。适合静态数据列表。
<template>
<div class="scroll-container">
<div class="scroll-list">
<div v-for="(item, index) in items" :key="index" class="item">
{{ item }}
</div>
</div>
</div>
</template>
<style>
.scroll-container {
height: 200px;
overflow: hidden;
}
.scroll-list {
animation: scroll 10s linear infinite;
}
@keyframes scroll {
0% { transform: translateY(0); }
100% { transform: translateY(-100%); }
}
</style>
使用JavaScript定时器
通过setInterval动态修改滚动位置,适合需要控制滚动速度的场景。

export default {
data() {
return {
items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'],
scrollPosition: 0
}
},
mounted() {
setInterval(() => {
this.scrollPosition += 1
if (this.scrollPosition > this.items.length * 30) {
this.scrollPosition = 0
}
}, 100)
}
}
使用第三方库
借助vue-seamless-scroll等专门库实现更复杂效果。

安装:
npm install vue-seamless-scroll
使用:
<template>
<vue-seamless-scroll
:data="items"
:class-option="option"
class="scroll-container">
<ul>
<li v-for="(item, index) in items" :key="index">{{ item }}</li>
</ul>
</vue-seamless-scroll>
</template>
<script>
import vueSeamlessScroll from 'vue-seamless-scroll'
export default {
components: { vueSeamlessScroll },
data() {
return {
items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'],
option: {
direction: 1,
limitMoveNum: 3
}
}
}
}
</script>
注意事项
- 动态数据更新时需重新计算滚动位置
- 移动端注意触摸事件冲突
- 性能优化:对于长列表建议使用虚拟滚动技术






