使用静态数据渲染诺干图片
如果图片分配的 ind 和轮播活动图片索引 imgInd 一致,则显示当前图片,否则隐藏
使用开发者工具查看并调试数据
样式略
<div class="swiper-static">
<img src="@/assets/swiper/swiper0.jpg" alt="" v-show="ind == 0">
<img src="@/assets/swiper/swiper1.jpg" alt="" v-show="ind == 1">
<img src="@/assets/swiper/swiper2.jpg" alt="" v-show="ind == 2">
<img src="@/assets/swiper/swiper3.jpg" alt="" v-show="ind == 3">
<img src="@/assets/swiper/swiper4.jpg" alt="" v-show="ind == 4">
<img src="@/assets/swiper/swiper5.jpg" alt="" v-show="ind == 5">
<img src="@/assets/swiper/swiper6.jpg" alt="" v-show="ind == 6">
</div>
import { ref } from 'vue';
const ind = ref(0);
let timer = setInterval(() => {
ind.value = (ind.value + 1) % 7;
}, 3000);
import { ref, onMounted, onUnmounted } from 'vue';
onMounted(() => {
timer = setInterval(() => {
ind.value = (ind.value + 1) % 7;
}, 3000);
})
onUnmounted(() => {
timer && clearInterval(timer);
})
改进2:图片增加异常处理
<img src="@/assets/swiper/swiper0.jpg" alt="" v-show="ind == 0" @error="errImg">
const errImg = (event) => {
console.error('Image failed to load:', event.target.src);
event.target.src = '@/assets/logo.png';
};