用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的盒子里,放的顺序是从上到下,且将溢出的部分隐藏,然后做一个从下往上的轮播,速度是每2秒500px,当list中数据轮播完之后,轮播停止,在轮播停止后过了10s往list当中push十条新数据过后继续轮播
时间: 2023-04-10 09:02:34 浏览: 85
可以使用以下代码实现:
```
<template>
<div class="container" style="width: 400px; height: 600px; overflow: hidden;">
<div class="list" v-for="(item, index) in list" :key="index" style="width: 400px; height: 200px;">
<p>{{ item.name }}</p>
<p>{{ item.event }}</p>
</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' }
],
currentIndex: 0,
timer: null
}
},
mounted() {
this.startCarousel()
},
methods: {
startCarousel() {
this.timer = setInterval(() => {
const container = document.querySelector('.container')
const list = document.querySelectorAll('.list')
const itemHeight = list[0].offsetHeight
const containerHeight = container.offsetHeight
const maxIndex = list.length - Math.floor(containerHeight / itemHeight)
if (this.currentIndex < maxIndex) {
this.currentIndex++
container.style.transform = `translateY(-${this.currentIndex * itemHeight}px)`
} else {
clearInterval(this.timer)
setTimeout(() => {
this.list.push(
{ name: 'csw11', event: 'hhhhh' },
{ name: 'csw12', event: 'hhhhh' },
{ name: 'csw13', event: 'hhhhh' },
{ name: 'csw14', event: 'hhhhh' },
{ name: 'csw15', event: 'hhhhh' },
{ name: 'csw16', event: 'hhhhh' },
{ name: 'csw17', event: 'hhhhh' },
{ name: 'csw18', event: 'hhhhh' },
{ name: 'csw19', event: 'hhhhh' },
{ name: 'csw20', event: 'hhhhh' }
)
this.currentIndex = 0
container.style.transform = `translateY(0px)`
this.startCarousel()
}, 10000)
}
}, 2000)
}
}
}
</script>
<style scoped>
.container {
transition: transform 0.5s;
}
</style>
```
这段代码使用了 Vue2 的模板语法,通过 v-for 指令渲染了 list 中的数据,每条数据放在宽 400px 高 200px 的盒子里,并将溢出的部分隐藏。然后使用 setInterval 实现了从下往上的轮播,速度是每 2 秒 500px,当 list 中数据轮播完之后,轮播停止,在轮播停止后过了 10s 往 list 当中 push 十条新数据过后继续轮播。其中,使用了 CSS 的 transform 属性实现了平滑的滚动效果。
阅读全文