用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 15:04:16 浏览: 75
可以使用以下代码实现:
```html
<template>
<div class="container" :style="{ height: containerHeight + 'px' }">
<div class="list" :style="{ transform: 'translateY(' + translateY + 'px)' }">
<div class="item" v-for="(item, index) in list" :key="index" :style="{ top: (index * itemHeight) + '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' },
],
itemHeight: 200,
containerHeight: 600,
translateY: 0,
};
},
mounted() {
setInterval(() => {
this.translateY -= this.itemHeight;
if (this.translateY < -(this.list.length * this.itemHeight - this.containerHeight)) {
this.translateY = 0;
}
}, 5000);
},
};
</script>
<style scoped>
.container {
width: 400px;
overflow: hidden;
position: relative;
}
.list {
position: absolute;
transition: transform 1s;
}
.item {
width: 400px;
height: 200px;
position: absolute;
overflow: hidden;
}
.name {
font-size: 24px;
font-weight: bold;
}
.event {
font-size: 18px;
}
</style>
```
在这个代码中,我们使用了 Vue 的模板语法来渲染列表,并使用了 CSS 来控制每个数据项的样式。在 mounted 钩子函数中,我们使用 setInterval 来实现轮播效果,每隔 5 秒钟将 translateY 值减去一个 itemHeight,当 translateY 值小于 -(list.length * itemHeight - containerHeight) 时,将 translateY 值重置为 0,实现循环轮播的效果。
阅读全文