1.參考github地址https://github.com/surmon-china/vue-awesome-swipercss
首先安裝插件vue
npm install vue-awesome-swiper --save(建議安裝2.6.7穩定版本)即
npm install vue-awesome-swiper@2.6.7 --save
2.在main.js引入插件git
import Vue from 'vue'
//引入插件
import VueAwesomeSwiper from 'vue-awesome-swiper'
// require styles 引入樣式 import 'swiper/dist/css/swiper.css' //在全局使用該插件 Vue.use(VueAwesomeSwiper, /* { default global options } */)
3.編寫輪播組件github
<!-- The ref attr used to find the swiper instance --> <template> <swiper :options="swiperOption" ref="mySwiper" @someSwiperEvent="callback"> <!-- slides --> <swiper-slide>I'm Slide 1</swiper-slide> <swiper-slide>I'm Slide 2</swiper-slide> <swiper-slide>I'm Slide 3</swiper-slide> <swiper-slide>I'm Slide 4</swiper-slide> <swiper-slide>I'm Slide 5</swiper-slide> <swiper-slide>I'm Slide 6</swiper-slide> <swiper-slide>I'm Slide 7</swiper-slide> <!-- Optional controls 用於顯示滾動葉下方的小點,須要在組件date裏面將class名配置進去--> <div class="swiper-pagination" slot="pagination"></div>
//下面兩個標籤爲輪播圖兩側兩個箭頭,能夠註釋掉 <div class="swiper-button-prev" slot="button-prev"></div> <div class="swiper-button-next" slot="button-next"></div>
//輪播圖下方滾動條,能夠註釋掉 <div class="swiper-scrollbar" slot="scrollbar"></div> </swiper> </template> <script> export default { name: 'carrousel', data() { return { swiperOption: {
swiperOption: ".swiper-pagination" // some swiper options/callbacks // 全部的參數同 swiper 官方 api 參數 // ... } } }, computed: { swiper() { return this.$refs.mySwiper.swiper } }, mounted() { // current swiper instance // 而後你就可使用當前上下文內的swiper對象去作你想作的事了 console.log('this is current swiper instance object', this.swiper) this.swiper.slideTo(3, 1000, false) } } </script>
<style lang="stylus" scoped>
//樣式穿透,對當前wraper下面的全部出現子組件swiper-pagination-bullet-active的樣式全都改變,達到不受scoped限制的目的
.wraper >>> .swiper-pagination-bullet-active background-color #fff .wraper overflow hidden widows: 100% height: 0 padding-bottom 37.8% .swiper-img width: 100% /*height: 0*/ /*padding-bottom: */</style>