npm install vue-awesome-swiper --save
import Vue from 'vue'
import VueAwesomeSwiper from 'vue-awesome-swiper'
// require styles
import 'swiper/dist/css/swiper.css'
Vue.use(VueAwesomeSwiper, /* { default global options } */)
<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>
<!-- 以下内容有需要再进行添加 -->
<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: {
// 所有的参数同 swiper 官方 api 参数
notNextTick: true,
autoplay: true, // 自动播放设置
speed: 1000, // 自动播放时间间隔
loop: true, // 开启无限循环播放
//显示分页
pagination: {
el: '.swiper-pagination',
clickable: true
},
//切换模式 横屏或者竖屏
// direction : 'vertical',
//设置自动播放速度
autoplay: {
disableOnInteraction: false,
delay: 4000
},
//设置点击箭头
paginationClickable: true,
prevButton: '.swiper-button-prev',
nextButton: '.swiper-button-next',
//设置同屏显示的数量,默认为1,使用auto是随意的意思。
slidesPerView: 1,
//开启鼠标滚轮控制Swiper切换。可设置鼠标选项,或true使用默认值。
mousewheel: true,
//默认为false,普通模式:slide滑动时只滑动一格,并自动贴合wrapper,设置为true则变为free模式,slide会根据惯性滑动可能不止一格且不会贴合。
// freeMode:true
}
}
},
computed: {
swiper() {
return this.$refs.mySwiper.swiper
}
},
mounted() {
console.log('this is current swiper instance object', this.swiper)
// 跳转到第三块,即 跳转到内容 <swiper-slide>I'm Slide 3</swiper-slide>
this.swiper.slideTo(3, 1000, false)
}
}
</script>