swiper移动端左右滑动与连接同时触发解决方案

  swiper  api中有自己的属性preventClicks可以控制不同时触发,但是经自己测试,只适用于pc端,移动端仍然有问题,以下为
自己的实现方式,仅供参考。项目已经放到资源,下载可以看。
var startY,startX,endX,endY;
var mySwiper = new Swiper('.swiper-container',{
    preventClicks : true,
    pagination: '.swiper-pagination',
    paginationClickable: '.swiper-pagination',
    preventLinksPropagation : true,
    spaceBetween: 30,
    effect: 'fade',
    onTouchStart: function(swiper,event){
        var touch = event.touches[0];
        startY = touch.pageY;
        startX = touch.pageX;
    },
    onTouchMove: function(swiper,event){
        var touch = event.touches[0];
        endX = touch.pageX-startX;
        endY = touch.pageY-startY;
    },
    onTouchEnd: function(swiper){
        if(Math.abs(endX)>5){
            endX=0;
            return false;
        }else{
            var href;
           switch (swiper.index){
               case 1:
                   href = "https://m.jrj.com.cn/yi/prd/prdxq?type=sr";
               case 2:
                   href = "https://m.jrj.com.cn/yi/prd/prdxq?type=sr";
               case 3:
                   href = "https://m.jrj.com.cn/yi/prd/prdxq?type=sr";
               case 4:
                   href = "https://m.jrj.com.cn/yi/prd/prdxq?type=sr";
            }
            self.location = href;
            endX=0;
        }
    }

});

HTML

<div class="swiper-container swiper-container-horizontal"   >
    <div class="swiper-wrapper" style="transition-duration: 0ms; transform: translate3d(0px, 0px, 0px);">
        <div class="swiper-slide blue-slide swiper-slide-active"  style="background-image:url(https://newimg.jrj.com.cn/2016/12/20161216170436288.jpg)">
            <span style="text-align: center;display: inline-block;width: 100%;height: 100%;" ></span>
        </div>
        <div class="swiper-slide red-slide swiper-slide-next" style="background-image:url(https://newimg.jrj.com.cn/2016/12/20161216170435896.jpg)">
            <span style="text-align: center;display: inline-block;width: 100%;height: 100%;" ></span>
          </div>
        <div class="swiper-slide orange-slide" style="background-image:url(https://newimg.jrj.com.cn/2016/12/20161216170406833.jpg)" >
            <span style="text-align: center;display: inline-block;width: 100%;height: 100%;"></span>
        </div>
        <div class="swiper-slide orange-slide" style="background-image:url(https://newi0.jrj.com.cn/bxcsv2016/banner-lb.png)" >
            <span style="text-align: center;display: inline-block;width: 100%;height: 100%;"></span>
        </div>
    </div>
    <div class="swiper-pagination swiper-pagination-white"></div>
</div>

效果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值