码迷,mamicode.com
首页 > 移动开发 > 详细

jquery——移动端touch事件

时间:2018-05-26 16:43:43      阅读:6748      评论:0      收藏:0      [点我收藏+]

标签:col   art   original   坐标   end   鼠标   tar   获取   prevent   

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

// 判断默认行为是否可以被禁用
    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事件

标签:col   art   original   坐标   end   鼠标   tar   获取   prevent   

原文地址:https://www.cnblogs.com/end-emptiness/p/9093104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!