vue实现上下滚动列表
vue实现上下滚动列表
使用Vue实现上下滚动列表可以通过多种方式完成,包括CSS动画、JavaScript定时器或第三方库。以下是几种常见方法:
使用CSS动画实现滚动
通过CSS的@keyframes和transform属性实现平滑滚动效果。
<template>
<div class="scroll-container">
<div class="scroll-content">
<div v-for="(item, index) in list" :key="index" class="scroll-item">
{{ item }}
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
list: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']
};
}
};
</script>
<style>
.scroll-container {
height: 200px;
overflow: hidden;
}
.scroll-content {
animation: scroll 10s linear infinite;
}
@keyframes scroll {
0% {
transform: translateY(0);
}
100% {
transform: translateY(-100%);
}
}
.scroll-item {
height: 40px;
line-height: 40px;
}
</style>
使用JavaScript定时器实现滚动
通过setInterval动态修改列表位置,实现更灵活的控制。

<template>
<div class="scroll-container" ref="container">
<div class="scroll-content" :style="{ transform: `translateY(${offset}px)` }">
<div v-for="(item, index) in list" :key="index" class="scroll-item">
{{ item }}
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
list: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'],
offset: 0,
timer: null
};
},
mounted() {
this.startScroll();
},
beforeDestroy() {
clearInterval(this.timer);
},
methods: {
startScroll() {
this.timer = setInterval(() => {
this.offset -= 1;
if (this.offset <= -this.$refs.container.clientHeight) {
this.offset = 0;
}
}, 30);
}
}
};
</script>
<style>
.scroll-container {
height: 200px;
overflow: hidden;
}
.scroll-item {
height: 40px;
line-height: 40px;
}
</style>
使用第三方库vue-seamless-scroll
vue-seamless-scroll是一个专门用于实现无缝滚动的Vue组件库。
安装依赖:

npm install vue-seamless-scroll
使用示例:
<template>
<vue-seamless-scroll :data="list" class="scroll-container">
<div v-for="(item, index) in list" :key="index" class="scroll-item">
{{ item }}
</div>
</vue-seamless-scroll>
</template>
<script>
import vueSeamlessScroll from 'vue-seamless-scroll';
export default {
components: { vueSeamlessScroll },
data() {
return {
list: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']
};
}
};
</script>
<style>
.scroll-container {
height: 200px;
overflow: hidden;
}
.scroll-item {
height: 40px;
line-height: 40px;
}
</style>
实现无限循环滚动
对于需要无限循环的场景,可以通过复制列表数据并动态调整位置实现。
<template>
<div class="scroll-container" ref="container">
<div class="scroll-content" :style="{ transform: `translateY(${offset}px)` }">
<div v-for="(item, index) in extendedList" :key="index" class="scroll-item">
{{ item }}
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
list: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'],
extendedList: [],
offset: 0,
timer: null
};
},
mounted() {
this.extendedList = [...this.list, ...this.list];
this.startScroll();
},
beforeDestroy() {
clearInterval(this.timer);
},
methods: {
startScroll() {
this.timer = setInterval(() => {
this.offset -= 1;
if (this.offset <= -this.$refs.container.clientHeight) {
this.offset = 0;
}
}, 30);
}
}
};
</script>
<style>
.scroll-container {
height: 200px;
overflow: hidden;
}
.scroll-item {
height: 40px;
line-height: 40px;
}
</style>
以上方法可以根据实际需求选择,CSS动画适合简单场景,JavaScript定时器提供更多控制,第三方库则简化了开发流程。






