vue引用vue-awesome-swiper使用loop:true时点击事件失效问题处理

vue引用vue-awesome-swiper使用loop:true时点击事件失效问题处理

Swiper的API文档:API链接
个人使用的swiper版本为3.1.1,个人建议使用3.1.1,使用人数最多,避免踩更多的坑

处理方式一

利用swiper中v-for内的控件,将数据以属性的形式保存的内部dom上,再利用swiper的点击事件得到当前的dom,通过dom的.target.getAttribute找到当前dom想要传递的数据。

<div class="lunbo ">
        <!-- 切换上一张轮播图按钮 ************** start ******************** -->
        <div
          class="swiper-prev pre"
          style="width:3%;float:left;margin-right:2%;"
        >
          <img src="@/assets/images/pre.png" alt="" />
        </div>
        <!-- 切换上一张轮播图按钮 ************** end ******************** -->
        <!-- 轮播图 ************** start ******************** -->
        <div class="lunbo monthSwiper"
          style="width:90%;margin: 0 auto;float:left;position:relative">
          <swiper ref="mySwiper" :options="swiperOption" v-if="tupianList.length">
            <swiper-slide
              v-for="(item, index) in tupianList"
              :key="index"
              :pageLength="tupianList.length"
              :index="index"
              ><img
                :src="item.pictureUrl"
                alt=""
                style="width:95%;height:10.98vw;display:block;cursor: pointer;"
                :data-id="item.id"
                :data-type="4"
                :data-itemType="item.type"
              />
            </swiper-slide>
          </swiper>
        </div>
        <!-- 轮播图 ************** end ******************** -->
        <!-- 切换下一张轮播图按钮 ************** start ******************** -->
        <div class="swiper-next pre"
          style="width:3%;float:left;margin-left:2%;">
          <img src="@/assets/images/next.png" alt="" />
        </div>
        <!-- 切换下一张轮播图按钮 ************** end ******************** -->
      </div>
<script>
// 版本不同,引用写法不同,swiper,swiperSlide的首字母大小写也会影响
import { swiper, swiperSlide } from 'vue-awesome-swiper';
// css引用不同,或者'swiper/dist/css/swiper.css';
import 'swiper/swiper-bundle.css' 
export default {
  components: {
    swiper,
    swiperSlide
  },
  data() {
    return {
      tupianList: [],
      swiperOption: {
        navigation: {
          nextEl: '.swiper-next',
          prevEl: '.swiper-prev',
        },
        sleep:100,
        loop: true,
        loopedSlides:4,
        loopPreventsSlide:true,
        autoplay: true,
        slidesPerView:4,
        preventLinksPropagation: false,
        on: {
          click: (v) => {
            console.log(v)
            let id = v.target.getAttribute('data-id')
            let type = v.target.getAttribute('data-type')
            let itemtype = v.target.getAttribute('data-itemType')
            console.log(id,type,itemtype)
            if (itemtype != '无跳转') {
              this.$router.push({
                path: '/xiangqing'
              })
            }
          }
        }
      }
      
    }
  }
}
</script>

处理方式二

利用vue的data保存swiper的数据,再利用swiper的点击事件得到下标,找到指定下标的swiper数据。
详细内容参考:引文链接一引文链接二

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值