解决vue-awesome-swiper点击slide选中错误的问题

解决vue-awesome-swiper点击slide选中错误的问题

问题描述

在使用loop: true模式下: 动态循环数据 ,轮播有的点击生效,有的不生效。所以不能直接在dom上进行绑定事件,在swiper上进行绑定。

解决方法

html代码如下:

<swiper ref="mySwiper" :options="swiperOptions">
              <swiper-slide v-for="(item, index) in noticeList" :key="index">
                <div class="notice-item">
                  <div class="notice-new" v-if="index === 0">NEW</div>
                  <div class="notice-tips" v-else></div>
                  <p class="notice-txt">{{item.noticeTitle}}</p>
                  <div class="notice-time">{{item.updateTime ? parseTime(item.updateTime, '{y}-{m}-{d}') : parseTime(item.createTime, '{y}-{m}-{d}')}}</div>
                </div>
              </swiper-slide>

            </swiper>
data() {
	let _this = this;
	return {
		swiperOptions: {
        direction : 'vertical',
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev',
        },
        loop : true,
        autoplay: {
          delay: 2000,
          stopOnLastSlide: false,
          disableOnInteraction: false,
        },
        slidesPerView: 4,
        spaceBetween: 0,
        observer: true,
        observeParents: true,
        on: {
          click : function () { // 在这里点击会触发整个slide
            _this.handleInvite(this);
          }
        }
      },
	}
}

由于是loop:true模式,必须要重新计算,才能获取准确的下标。

handleInvite(vm) {
      let index = vm.clickedIndex - vm.activeIndex + vm.realIndex; // 由于swiper是loop:true模式,需重新计算才可得到真实的index下标
      if (index === this.noticeList.length) {
        index = 0;
      } else if (index === this.noticeList.length +1) {
        index = 1;
      } else if (index === this.noticeList.length +2) {
        index = 2;
      }
      // 得到的index就是点击的item在实际数组中的下标index
      this.noticeDetail(this.noticeList[index])
    },

这样就解决问题啦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值