用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' } ],每条数据放在宽400px高200px的盒子里,放的顺序是从上到下,且将溢出的部分隐藏,然后做一个从下往上的轮播,速度是每5秒600px,并且每过10s会通过定时器向list当中push3条数据,当轮播到最后一条数据的时候轮播停止,当轮播已经停止后list当中更新了新数据的时候从轮播图底部继续轮播
时间: 2023-04-12 13:00:36 浏览: 85
可以使用以下代码实现:
HTML:
```
<div class="container">
<div class="list-wrapper" :style="{height: listHeight + 'px'}">
<div class="list-item" v-for="(item, index) in list" :key="index" :style="{top: itemTop(index) + 'px'}">
<div class="item-name">{{ item.name }}</div>
<div class="item-event">{{ item.event }}</div>
</div>
</div>
</div>
```
CSS:
```
.container {
width: 400px;
height: 600px;
overflow: hidden;
position: relative;
}
.list-wrapper {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
transition: top 0.5s;
}
.list-item {
width: 400px;
height: 200px;
position: absolute;
left: 0;
overflow: hidden;
}
.item-name {
font-size: 20px;
font-weight: bold;
margin-bottom: 10px;
}
.item-event {
font-size: 16px;
}
```
JavaScript:
```
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' }
],
listHeight: 1600,
currentIndex: 0,
timer: null
}
},
mounted() {
this.startCarousel()
setInterval(() => {
this.addItems()
}, 10000)
},
methods: {
itemTop(index) {
return index * 200
},
startCarousel() {
this.timer = setInterval(() => {
this.currentIndex++
if (this.currentIndex >= this.list.length) {
clearInterval(this.timer)
} else {
this.listHeight -= 200
this.$nextTick(() => {
this.$refs.listWrapper.style.top = -this.currentIndex * 200 + 'px'
})
}
}, 5000)
},
addItems() {
const newItems = [
{ name: 'csw9', event: 'hhhhh' },
{ name: 'csw10', event: 'hhhhh' },
{ name: 'csw11', event: 'hhhhh' }
]
this.list = [...this.list, ...newItems]
this.listHeight += 600
this.startCarousel()
}
}
}
```
在这个代码中,我们使用了一个容器 div 来包裹整个轮播图,使用了一个 div 来渲染列表,使用了 v-for 指令来循环渲染每一条数据。我们使用了 CSS 来设置每个列表项的样式,包括宽度、高度、位置和溢出隐藏等。我们使用了 JavaScript 来实现轮播图的滚动效果,包括定时器、计算列表高度和滚动距离等。我们还使用了定时器来定期添加新的数据到列表中。
阅读全文
相关推荐










