<el-carousel :interval="5000" arrow="always"> <el-carousel-item v-for="item in 4" :key="item"> <img src="../../assets/pictures/1.jpg" alt=""> <img src="../../assets/pictures/2.png" alt=""> <h3>{{ item }}</h3> </el-carousel-item> </el-carousel> 更改代码使照片可轮流播放
时间: 2023-07-19 13:40:48 浏览: 113
实现轮换图片的js,可实现图片轮换播放
可以将每个 `el-carousel-item` 中的图片放在一个数组中,然后在每个组件的 `mounted` 钩子函数中,使用 `setInterval` 定时器来切换图片。代码如下:
```html
<template>
<el-carousel :interval="5000" arrow="always">
<el-carousel-item v-for="item in 4" :key="item">
<img :src="images[index]" alt="">
<h3>{{ item }}</h3>
</el-carousel-item>
</el-carousel>
</template>
<script>
export default {
data() {
return {
images: [
["../../assets/pictures/1.jpg", "../../assets/pictures/2.png"],
["../../assets/pictures/3.jpg", "../../assets/pictures/4.png"],
["../../assets/pictures/5.jpg", "../../assets/pictures/6.png"],
["../../assets/pictures/7.jpg", "../../assets/pictures/8.png"],
],
index: 0,
};
},
mounted() {
setInterval(() => {
this.index = (this.index + 1) % this.images.length;
}, 5000);
},
};
</script>
```
这样就可以实现照片轮流播放了。
阅读全文