vue封装一个swiper组件
首先在页面内引入swiper脚本,包括css、js
<!-- swiper --> <link href="https://cdn.staticfile.org/Swiper/4.5.1/css/swiper.min.css" rel="stylesheet">
<!-- swiper --> <script src="https://cdn.staticfile.org/Swiper/4.5.1/js/swiper.min.js"></script>
另外需要在eslint中配置,将swiper对象配置成全局对象

在components文件中写一个swiper组件

<template>
<div>
<div :class="swpName +' swiperBox '+ className">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="(item,key) in list" :key="'swiperSlide'+key">
<a v-if="item.link != undefined" :href="item.link" class="bannerItem">
<img :src="item.src" alt="">
</a>
<div v-else class="bannerItem"><img :src="item.src" alt=""></div>
</div>
</div>
<div v-if="isPrevNext == true" :class="swpName + 'prev swiper-button-prev swiper-button-'+ prevNextColor" slot="button-prev"></div>
<div v-if="isPrevNext == true" :class="swpName + 'next swiper-button-next swiper-button-'+ prevNextColor" slot="button-next"></div>
<div v-if="isPagination == true" :class="'swiper-pagination'"></div>
</div>
</div>
</template>
<script>
export default {
props: {
//轮播图列表数据
list: {
type: Object,
default: function(){
return []
}
},
//样式名称
className: {
type: String,
default: ''
},
prevNextColor: {
type: String,
default: ''
},
//是否显示圆点
isPagination: {
type: Boolean,
default: false
},
//是否显示左右箭头
isPrevNext: {
type: Boolean,
default: false
},
//自动播放延迟多少毫秒
delay: {
type: String,
default: '3000'
},
//是否循环切换
loop: {
type: Boolean,
default: true
},
//是否自动播放
autoPlay: {
type: Boolean,
default: true
},
// 一屏显示几个
slidesPerView:{
type:Number,
default:1
},
//每个轮播间的间隔距离
spaceBetween:{
type:Number,
default:0
},
//导航点样式
paginationType:{
type:String,
default:''
}
},
data(){
return{
swpName: this.roundString(),
MySwiper:null,//swiper实例
}
},
mounted(){
var self=this;
self.MySwiper = new Swiper ('.'+ self.swpName,{
init: true,
observer:true,
observeParents:true,
slidesPerView: self.slidesPerView,
spaceBetween: self.spaceBetween,
keyboard: {
enabled: true,
},
loop: self.loop,
autoplay:self.autoPlay? {delay: self.delay,disableOnInteraction: false}:false,
pagination: {
el: '.swiper-pagination',
clickable: true,
type:self.paginationType?self.paginationType:'bullets'
},
navigation: {
nextEl: '.'+self.swpName + 'prev',
prevEl: '.'+self.swpName + 'next'
},
});
},
methods: {
roundString() {
let str = "";
let chars = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
chars.forEach(() => {
str += chars[Math.ceil(Math.random()*25)]
});
return str
}
},
}
</script>
<style lang="scss" scoped>
@import url("../assets/css/swiperBullet.css");
</style>
注意到上面组件内需要引入一个swiperBullet.css样式,之所以这样引入,是可以自定义导航点
swiperBullet.css放在assets文件夹下
.swiperBox{ width:100%; position: relative; overflow: hidden; } .swiperBox .bannerItem img{ height:auto; width:100%; } .swiperBox .bannerItem{ width:100%; text-align: center; } .swiperBox .bannerItem img{ height:auto; width:100%; } .swiper-pagination{ position: absolute; } /* 轮播导航点配置 */ .swiper-pagination .swiper-pagination-bullet { width: 0.1rem; height: 0.1rem; text-align: center; line-height: 20px; font-size: 12px; color:#000; opacity: 1; background: rgba(0,0,0,0.2); margin-right:20px; outline: none; } .swiper-pagination .swiper-pagination-bullet:last-child{ margin-right:0; } .swiper-pagination .swiper-pagination-bullet-active { color:#fff; background: #005aab; }
在页面中使用:
import SwiperCmp from "@/components/swiperComponent";
components:{SwiperCmp},
<div class="banner_box">
<SwiperCmp :list="bannerList" :isPagination="true" :className="'swiperClass'"/>
</div>
自定义导航点样式:
.banner_box{ /deep/.swiperClass{ height:3rem; .swiper-pagination{ bottom:1rem; //这里可以自定义导航点的样式 } } }
。

浙公网安备 33010602011771号