引用 Vue-Awesome-Swiper
安装
cnpm install vue-awesome-swiper -S
使用
全局导入:
import vueSwiper from 'vue-awesome-swiper'
/* 样式的话,我这里有用到分页器,就在全局中引入了样式 */
import 'swiper/dist/css/swiper.css'
Vue.use(vueSwiper);
组件导入:
import { swiper, swiperSlide } from "vue-awesome-swiper";
require("swiper/dist/css/swiper.css");
components: {
swiper,
swiperSlide
},
在template中使用:
<swiper :options="swiperOption" ref="mySwiper" >
<swiper-slide v-for="(item,index) in xxx" :key="index" >
<img :src="item.image" alt="" />
</swiper-slide>
<!-- 常见的小圆点 -->
<div class="swiper-pagination" slot="pagination" ></div>
</swiper>
在data中配置:
data() {
return {
xxx:["图片地址1","图片地址2"]
imgIndex: 1,
swiperOption: {
//是一个组件自有属性,如果notNextTick设置为true,组件则不会通过NextTick来实例化swiper,也就意味着你可以在第一时间获取到swiper对象,假如你需要刚加载遍使用获取swiper对象来做什么事,那么这个属性一定要是true
notNextTick: true,
//循环
loop: true,
//设定初始化时slide的索引
initialSlide: 0,
//自动播放
autoplay: {
delay: 1500, //滑动一次时间
stopOnLastSlide: false,
/* 触摸滑动后是否继续轮播 */
disableOnInteraction: false
},
//滑动方向
direction: "horizontal",
//小手掌抓取滑动
grabCursor: true,
//分页器设置
pagination: {
el: ".swiper-pagination",
clickable: true,
}
}
};
},
更多API可以查阅官网:https://www.swiper.com.cn/api/index.html