马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
在 Vue2 中使用 vue-awesome-swiper(Swiper)
项目中用到 vue-awesome-swiper 做轮播,以下是基本使用步骤。
第一步:安装依赖
第二步:引入组件

- import { swiper, swiperSlide } from "vue-awesome-swiper";
- import "swiper/dist/css/swiper.css";
- export default {
- components: {
- swiper,
- swiperSlide,
- },
- // ...
- }
复制代码
第三步:创建容器

- <swiper :options="swiperOption">
- <swiper-slide> slide 1 </swiper-slide>
- <swiper-slide> slide 2 </swiper-slide>
- <swiper-slide> slide 3 </swiper-slide>
- </swiper>
复制代码
第四步:创建配置对象

- swiperOption: {
- effect: "coverflow",
- grabCursor: true,
- centeredSlides: true,
- slidesPerView: "auto",
- coverflowEffect: {
- rotate: 0,
- stretch: -80, // slide 左右距离
- depth: 400, // slide 前后距离
- modifier: 0.5,
- slideShadows: false
- },
- loop: true,
- on: {
- click: function () {
- // 监听点击滑块事件
- // this.realIndex 是当前 swiper 实例的激活索引
- },
- slideChange: function () {
- console.log(this.realIndex);
- }
- }
- }
复制代码
swiper 的监听事件都可以在 on 配置对象中绑定,此时 this 指向 swiper 实例本身。
另一种绑定事件的方式
如果想通过 ref 获取 swiper 对象,可给组件绑定 ref,然后通过:
- this.$refs.myswiper.swiper
复制代码
获取实例。注意:this.$refs.myswiper.swiper.realIndex 即当前被激活滑块的索引值,this.slides[realIndex] 可获取当前滑块内容。
qidao123.com企服评测-应用市场 编辑提示
|