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

js判断移动端手势 上下左右滑动事件

时间:2017-09-13 15:16:03      阅读:932      评论:0      收藏:0      [点我收藏+]

标签:alert   asc   page   style   移动端   滑动   prevent   end   touch   

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="https://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
    </head>
    
    <body style="height: 900px;background: #ececec;">
    </body>
    <script type="text/javascript">
    
$("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 ( Math.abs(X) > Math.abs(Y) && X > 0 ) {
        alert("left 2 right");
    }
    else if ( Math.abs(X) > Math.abs(Y) && X < 0 ) {
        alert("right 2 left");
    }
    else if ( Math.abs(Y) > Math.abs(X) && Y > 0) {
        alert("top 2 bottom");
    }
    else if ( Math.abs(Y) > Math.abs(X) && Y < 0 ) {
        alert("bottom 2 top");
    }
    else{
        alert("just touch");
    }
});
    </script>
</html>

 

js判断移动端手势 上下左右滑动事件

标签:alert   asc   page   style   移动端   滑动   prevent   end   touch   

原文地址:http://www.cnblogs.com/wenb/p/7514673.html

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