用vue2在宽400px高600px的div里面用v-for渲染list: [ { name: 'csw1', event: 'hhhhh' }, { name: 'csw2', event: 'hhhhh' }, { name: 'csw3', event: 'hhhhh' }, { name: 'csw4', event: 'hhhhh' }, { name: 'csw5', event: 'hhhhh' }, { name: 'csw6', event: 'hhhhh' }, { name: 'csw7', event: 'hhhhh' }, { name: 'csw8', event: 'hhhhh' }, { name: 'csw9', event: 'hhhhh' }, { name: 'csw10', event: 'hhhhh' },, ],每条数据放在宽400px高200px的盒子里,放的顺序是从上到下,且将溢出的部分隐藏,然后做一个从下往上的循环无缝轮播,轮播到第十条数据的时候紧接着继续轮播第1条数据,速度是每5秒600px,轮播重新开始的第一下动画效果不生效
时间: 2023-04-09 20:04:19 浏览: 65
你可以使用以下代码实现:
```html
<template>
<div class="container">
<div class="list" :style="{transform: 'translateY(' + translateY + 'px)'}">
<div class="item" v-for="(item, index) in list" :key="index" :style="{top: (index * 200) + 'px'}">
<div class="name">{{ item.name }}</div>
<div class="event">{{ item.event }}</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
list: [
{ name: 'csw1', event: 'hhhhh' },
{ name: 'csw2', event: 'hhhhh' },
{ name: 'csw3', event: 'hhhhh' },
{ name: 'csw4', event: 'hhhhh' },
{ name: 'csw5', event: 'hhhhh' },
{ name: 'csw6', event: 'hhhhh' },
{ name: 'csw7', event: 'hhhhh' },
{ name: 'csw8', event: 'hhhhh' },
{ name: 'csw9', event: 'hhhhh' },
{ name: 'csw10', event: 'hhhhh' },
],
translateY: 0,
currentIndex: 0,
timer: null,
};
},
mounted() {
this.startLoop();
},
methods: {
startLoop() {
this.timer = setInterval(() => {
this.currentIndex++;
this.translateY = -this.currentIndex * 200;
if (this.currentIndex === this.list.length) {
this.currentIndex = 0;
this.translateY = 0;
}
}, 5000);
},
},
};
</script>
<style scoped>
.container {
width: 400px;
height: 600px;
overflow: hidden;
position: relative;
}
.list {
position: absolute;
transition: transform 0.5s;
}
.item {
width: 400px;
height: 200px;
position: absolute;
overflow: hidden;
}
.name {
font-size: 20px;
font-weight: bold;
}
.event {
font-size: 16px;
color: #999;
}
</style>
```
在这个代码中,我们使用了 Vue 的 v-for 指令来渲染列表,使用了 CSS 的 transform 属性来实现轮播效果。我们通过设置 translateY 属性来控制列表的位置,从而实现轮播。同时,我们使用了 setInterval 函数来定时更新列表的位置,从而实现自动轮播的效果。