vue实现轮播图自动切换
时间: 2023-09-17 14:12:41 浏览: 187
vue实现轮播图(滚动切换,无缝切换,自动切换,循环无回滚等)
Vue实现轮播图自动切换可以使用vue-awesome-swiper插件。首先,在项目中安装该插件,然后在需要使用轮播图的组件中引入该插件。接着,在data中定义swiperOption对象,设置相关参数,如自动播放时间间隔、轮播速度等。最后,在template中引入轮播图组件,使用v-for循环渲染图片列表,添加swiper-slide类名和图片地址作为属性即可实现轮播图自动切换。具体代码如下:
<template>
<div class="home-carousel">
<swiper :options="swiperOption">
<swiper-slide v-for="item in imgList" :key="item.id" :style="{backgroundImage: `url(${item.src})`}"></swiper-slide>
<div class="swiper-pagination" slot="pagination"></div>
</swiper>
</div>
</template>
<script>
import 'swiper/css/swiper.css'
import {Swiper, SwiperSlide} from 'vue-awesome-swiper'
export default {
name: 'HomeCarousel',
components: {
Swiper,
SwiperSlide
},
data: function () {
return {
imgList: [
{id: 1, src: require('@/assets/images/home-carousel/home-carousel-1.jpg')},
{id: 2, src: require('@/assets/images/home-carousel/home-carousel-2.jpg')},
{id: 3, src: require('@/assets/images/home-carousel/home-carousel-3.jpg')}
],
swiperOption: {
loop: true,
autoplay: {
delay: 3000,
disableOnInteraction: false
},
pagination: {
el: '.swiper-pagination'
},
speed: 600
}
}
}
}
</script>
阅读全文