使用jq实现手机上的touchmove效果

Touch事件简介
pc上的web页面鼠 标会产生onmousedown、onmouseup、onmouseout、onmouseover、onmousemove的事件,但是在移动终端如 iphone、ipod Touch、ipad上的web页面触屏时会产生ontouchstart、ontouchmove、ontouchend、ontouchcancel 事件,分别对应了触屏开始、拖拽及完成触屏事件和取消。
当按下手指时,触发ontouchstart;
当移动手指时,触发ontouchmove;
当移走手指时,触发ontouchend。
touchmove判断滑动方向代码如下:

$('body').bind('touchstart',function(e){
            startX = e.originalEvent.changedTouches[0].pageX,
            startY = e.originalEvent.changedTouches[0].pageY;
        });
        $('body').bind('touchmove',function(e){
            //获取滑动屏幕时的X,Y
            endX = e.originalEvent.changedTouches[0].pageX,
            endY = e.originalEvent.changedTouches[0].pageY;
            //获取滑动距离
            distanceX = endX-startX;
            distanceY = endY-startY;
            //判断滑动方向
            if(Math.abs(distanceX)>Math.abs(distanceY) && distanceX>0){
                console.log('往右滑动');
            }else if(Math.abs(distanceX)>Math.abs(distanceY) && distanceX<0){
                console.log('往左滑动');
            }else if(Math.abs(distanceX)<Math.abs(distanceY) && distanceY<0){
                console.log('往上滑动');
            }else if(Math.abs(distanceX)<Math.abs(distanceY) && distanceY>0){
                console.log('往下滑动');
            }else{
                console.log('点击未滑动');
            }

        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值