关于swiper插件在vue2的使用

[复制链接]
发表于 6 天前 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
在 Vue2 中使用 vue-awesome-swiper(Swiper)

项目中用到 vue-awesome-swiper 做轮播,以下是基本使用步骤。

第一步:安装依赖

  1. npm install swiper vue-awesome-swiper --save
  2. # 或指定版本
  3. npm install swiper [email protected] --save
复制代码


第二步:引入组件



  1. import { swiper, swiperSlide } from "vue-awesome-swiper";
  2. import "swiper/dist/css/swiper.css";

  3. export default {
  4.     components: {
  5.         swiper,
  6.         swiperSlide,
  7.     },
  8.     // ...
  9. }
复制代码


第三步:创建容器



  1. <swiper :options="swiperOption">
  2.     <swiper-slide> slide 1 </swiper-slide>
  3.     <swiper-slide> slide 2 </swiper-slide>
  4.     <swiper-slide> slide 3 </swiper-slide>
  5. </swiper>
复制代码


第四步:创建配置对象



  1. swiperOption: {
  2.     effect: "coverflow",
  3.     grabCursor: true,
  4.     centeredSlides: true,
  5.     slidesPerView: "auto",
  6.     coverflowEffect: {
  7.         rotate: 0,
  8.         stretch: -80,   // slide 左右距离
  9.         depth: 400,     // slide 前后距离
  10.         modifier: 0.5,
  11.         slideShadows: false
  12.     },
  13.     loop: true,
  14.     on: {
  15.         click: function () {
  16.             // 监听点击滑块事件
  17.             // this.realIndex 是当前 swiper 实例的激活索引
  18.         },
  19.         slideChange: function () {
  20.             console.log(this.realIndex);
  21.         }
  22.     }
  23. }
复制代码


swiper 的监听事件都可以在 on 配置对象中绑定,此时 this 指向 swiper 实例本身。

另一种绑定事件的方式

如果想通过 ref 获取 swiper 对象,可给组件绑定 ref,然后通过:

  1. this.$refs.myswiper.swiper
复制代码


获取实例。注意:this.$refs.myswiper.swiper.realIndex 即当前被激活滑块的索引值,this.slides[realIndex] 可获取当前滑块内容。

qidao123.com企服评测-应用市场 编辑提示
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表