swiper获取当前数组滑动的inex(vue)

最近有一个需求,上方是一个swiper,下方是商品数组,当滑动上方的swiper下方的列表需要跟着一起变化,这时候就需要获取当前滑动的swiper数组的index,再进行相关操作,实现方式如下:

import Swiper from "swiper";

import "swiper/dist/css/swiper.css";

import "swiper/dist/js/swiper.min";

export default {

  data() {

    return {

      merchantsZoneList: [],

      zoneId: "",

      currentSlide: 0,

    };

  },

  created() {

    //调用获取swiper list

    this.getMoaZone();

  },

  methods: {

    dataCarousel(data) {

      //此处需要改变一下this指向,因为此处的this是指向swiper

      let that = this;

      //初始化一个swiper

      this.mySwiper = new Swiper(".swiper-container", {

        //配置项

        loop: true,

        effect: "coverflow",

        spaceBetween: 20,

        grabCursor: true,

        centeredSlides: true,

        slidesPerView: "auto",

        initialSlide: 0,

        observer: true,

        observerParents: true,

        autoplay: 100,

        pagination: {

          el: ".swiper-pagination",

          clickable: true,

        },

        coverflowEffect: {

          rotate: 0,

          slideShadows: false,

        },

        on: {

          //滑动swiper使用的方法

          transitionEnd: function() {

            //当需要滑动的swiper选项等于当前的swiper选项,就不能调用方法

            if (this.activeIndex == that.currentSlide) {

              console.log("不能调用方法");

            } else {

              //只有相等才调用方法

              that.currentSlide = this.activeIndex;

              //获取当前swiper数组的index,data是从后台获取并传入的数组

              const index = (this.activeIndex + data.length) % data.length;

              //调用获取list方法数组

              that.zoneId = data[index].id;

              that.productList = [];

              that.pageNum = 0;

              that.getProductList();

            }

          },

        },

      });

    },

    async getZoneList() {

      //从获取swiper数组

      let res = await zoneList({});

      //获取swiper list赋值

      this.merchantsZoneList = res.data.ptZoneList;

      //使用异步方法调用swiper初始化并传入

      setTimeout(() => {

        this.dataCarousel(res.data.ptZoneList);

      }, 10);

    },

  },

};

### Vue Swiper 获取当前激活 Slide 的索引 在 Vue 中使用 `vue-awesome-swiper` 插件时,可以通过监听特定事件来获取当前激活的 `swiper-slide` 索引。由于 Swiper 3 版本不支持 `realIndex` 属性,在循环模式 (`loop`) 下需要转换 `activeIndex` 来获得实际索引。 #### 方法一:通过 `onInit` 和 `slideChangeTransitionEnd` 事件 可以在初始化选项中设置回调函数处理程序,当幻灯片切换结束时触发这些方法: ```javascript data() { return { swiperOption: { loop: true, on: { init: function(swiper) { console.log('初始活动索引:', swiper.realIndex); }, slideChangeTransitionEnd: function(swiper) { console.log('新活动索引:', swiper.realIndex); } } } }; } ``` 对于版本低于4.x的Swiper库,如果遇到没有 `realIndex` 的情况,则可以利用 `activeIndex` 并减去虚拟副本的数量得到真实的索引位置[^1]。 #### 方法二:直接访问实例属性 另一种方式是在模板内定义好 Swiper 组件之后,借助 `$refs` 访问组件内部维护的实际 Swiper 实例对象: ```html <template> <swiper ref="mySwiper" :options="swiperOptions"> <!-- slides here --> </swiper> </template> <script> export default { computed: { swiperInstance() { return this.$refs.mySwiper && this.$refs.mySwiper.swiper; } }, methods: { getCurrentIndex() { const swiper = this.swiperInstance; if (swiper) { console.log(`当前索引 ${swiper.realIndex}`); } } } }; </script> ``` 此代码片段展示了如何创建计算属性以便随时调用以返回最新的 Swiper 对象,并提供了一个名为 `getCurrentIndex()` 的方法用于打印当前的真实索引值[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值