1 npm 安装css
1 npm install vue-awesome-swiper --save
2在所用的组件中引入vue
1 import 'swiper/dist/css/swiper.css' 2 import { swiper, swiperSlide } from 'vue-awesome-swiper'
3 在components中进行注册vue-cli
1 components:{ 2 swiper, 3 swiperSlide 4 },
4 在vue-cli中组件的template中进行使用npm
1 <template> 2 <swiper :options="swiperOption"> 3 <swiper-slide v-for="(item,index) in slideImages"> 4 <a href="#" target="_blank"><img :src="item.imageUrl" /><span class="title">{{item.title}}</span></a> 5 </swiper-slide> 6 <div class="swiper-pagination swiper-pagination-bullets" slot="pagination"></div> 7 </swiper> 8 </template>
5 对轮播图的属性进行配置ide
1 data(){ 2 return { 3 swiperOption: { 4 autoplay: {//自动播放 5 delay: 2500, 6 disableOnInteraction: false 7 },
8 pagination: {//分页 9 el: '.swiper-pagination', 10 clickable: true, 11 renderBullet(index, className) {//自定义分页的按钮 12 return `<span class="${className} swiper-pagination-bullet-custom"></span>` 13 } 14 } 15 } 16 } 17 },
其中按钮的样式的css代码以下:oop
1 .swiper-pagination-bullet-custom { 2 width: 9px; 3 height: 9px; 4 text-align: center; 5 line-height: 20px; 6 font-size: 12px; 7 color: #000; 8 opacity: 1; 9 border-radius: 0; 10 background: #fff; 11 } 12 .swiper-pagination-bullet-custom.swiper-pagination-bullet-active { 13 color: #fff; 14 background: #a11703; 15 }
这样子轮播图就能够自动轮播啦!!!spa
遇到的问题:code
若是要实现无缝轮播,须要在swiperOption中添加以下代码component
1 swiperOption: { 2 autoplay: { 3 delay: 2500, 4 disableOnInteraction: false 5 }, 6 loop:true,//环装轮播 7 }
同时还要在<swiper>添加v-if控制环装轮播,不然不起做用blog
代码以下:
1 <swiper :options="swiperOption" ref="mySwiper" v-if="swiperList.length>1"> 2 <!--用v-if控制loop环状轮播,不然不起做用--> 3 <swiper-slide v-for="(item,index) in swiperList" 4 :index="index" :key="item.index" class="swiper_box"> 5 <div class="goodsimg"> 6 <img :src=imgURL+item.goodsPicture alt="" /> 7 </div> 9 </swiper-slide> 10 </swiper>