jquery——移动端touch事件

Posted 颓废之风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jquery——移动端touch事件相关的知识,希望对你有一定的参考价值。

首先为了防止事件触发默认行为,我们需要去禁止,安全的禁止方法:

// 判断默认行为是否可以被禁用
    if (e.cancelable) {
        // 判断默认行为是否已经被禁用
        if (!e.defaultPrevented) {
            e.preventDefault();
        }
}   

三个事件:

$("body").on("touchstart", function(e) {

   e.preventDefault();

});

$("body").on("touchend", function(e) {

   e.preventDefault();

});

$("body").on("touchmove", function(e) {

   e.preventDefault();

});

 

移动开始和结束的坐标获取:

startX = e.originalEvent.changedTouches[0].pageX;

startY = e.originalEvent.changedTouches[0].pageY;

moveEndX = e.originalEvent.changedTouches[0].pageX;

moveEndY = e.originalEvent.changedTouches[0].pageY;

 

样例:

$("body").on("touchstart", function(e) {

    e.preventDefault();

    startX = e.originalEvent.changedTouches[0].pageX,

    startY = e.originalEvent.changedTouches[0].pageY;

});

$("body").on("touchmove", function(e) {

    e.preventDefault();

    moveEndX = e.originalEvent.changedTouches[0].pageX,

    moveEndY = e.originalEvent.changedTouches[0].pageY,

    X = moveEndX - startX,

    Y = moveEndY - startY;

    if ( X > 0 ) {

       alert(‘向左滑动‘);

    }

});

 

 

对应pc端鼠标操作:

touchstart  ——>   mousesdown

touchend  ——>   mouseup

touchmove  ——>   mousemove

以上是关于jquery——移动端touch事件的主要内容,如果未能解决你的问题,请参考以下文章