Vue项目 --- 轮播图插件swiper
2018-07-11 本文已影响0人
V火力全开
swiper安装:
npm install vue-awesome-swiper --save
引入
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
Vue.use(VueAwesomeSwiper)
使用例子:
<template>
<div class="wrapper">
<swiper :options="swiperOption" v-if="showSwiper">
<!-- slides -->
<swiper-slide v-for="item of list" :key="item.id">
<img class="swiper-img" :src="item.imgUrl" alt="">
</swiper-slide>
<!-- Optional controls -->
<div class="swiper-pagination" slot="pagination"></div>
</swiper>
</div>
</template>
<script>
export default {
name: 'HomeSwiper',
props: {
list: Array
},
data () {
return {
swiperOption: {
pagination: '.swiper-pagination',
autoplay: 3000,
loop: true
}
}
},
computed: {
showSwiper () {
return this.list.length
}
}
}
</script>
<style lang="stylus" scoped>
.wrapper >>> .swiper-pagination-bullet-active
background: #fff
.wrapper
overflow: hidden
width: 100%
height: 0
padding-bottom: 31.25%
background: #eee
.swiper-img
width: 100%
</style>
轮播控制
当不希望自动切换的时候
autoplay: false,
当轮播到头的时候,不希望接着循环播放
loop: false
具体详细配置信息查看:http://www.swiper.com.cn
关于样式
这里要注意,我们想覆盖掉swiper-pagination-bullet-active
原有的样式,但是样式并不在当前页,因为style
设置了scoped
的原因,所以要用3个箭头进行一个穿透,就不受scoped
的限制了
.wrapper >>> .swiper-pagination-bullet-active
background: #fff