1.安装下载
npm install swiper vue-awesome-swiper --save
2.引入
引入awesome-swiper
支持全局引入和在组件中引入2种方式,如果项目中只有一个轮播的话推荐在组件中引入,有多个轮播则推荐在全局中引入。
(1)全局引入方式,在main.js中添加以下代码
import VueAwesomeSwiper from 'vue-awesome-swiper'
// import style
import 'swiper/swiper-bundle.css'//脚手架3对应的css样式引入
Vue.use(VueAwesomeSwiper, /* { default options with global component } */)
(2).局部引入方式,在当个组件中引入
import { Swiper, SwiperSlide, directive } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
export default {
components: {
Swiper,
SwiperSlide
},
directives: {
swiper: directive
}
}
3.使用
<div class="swiper">
<swiper :options="swiperOption" class="swiper-menu">
<!-- slides 列表项 -->
<swiper-slide>
<img src="../assets/images/banner1.jpg" alt />
</swiper-slide>
<swiper-slide>
<img src="../assets/images/banner2.jpg" alt />
</swiper-slide>
<swiper-slide>
<img src="../assets/images/banner3.jpg" alt />
</swiper-slide>
<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>
<div class="swiper-pagination change" slot="pagination"></div>
</swiper>
</div>
data() {
return {
swiperOption: {
initialSlide: 0,
notNextTick: true,
loop: true, // 是否循环轮播
autoplay: {
delay: 2000,
disableOnInteraction: false
},
//滑动速度
speed: 2000,
//滑动方向
direction: "horizontal",
//小手掌抓取滑动
pagination:'.swiper-pagination',//这里修改
grabCursor: true,
prevButton: ".swiper-button-prev",
nextButton: ".swiper-button-next",
paginationClickable: true,
mousewheelControl: true
}
};
},
mounted () {
// 这边就可以使用swiper这个对象去使用swiper官网中的那些方法
this.swiper.slideTo(1, 1000, false)
},
computed: {
swiper () {
return this.$refs.mySwiper.swiper
}
},