• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
孤独的DNA
博客园    首页    新随笔    联系   管理    订阅  订阅

js移动端的滑动事件

<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <!--<meta name="viewport" content="width=device-width, initial-scale=1">-->
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
        <title>touch event</title>
        <style type="text/css" media="screen">
.spirit {             
    position: absolute;
    width: 50px;
    height: 50px;
    background-color: red;
    -webkit-border-radius: 25px;
}
.page {
    font-size: 24px;
    font-weight: bold;
    text-align: center;
}

        </style>
    </head>
    <body style="margin:0;padding:0;background:#ccc;">

        <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
        <script type="text/javascript">
            var windowHeight = $(window).height(),
                $body = $("body");
            $body.css("height", windowHeight);
            var startX, startY, moveEndX, moveEndY, X, Y;

$("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("像右滑动");
    }
    else if ( Math.abs(X) > Math.abs(Y) && X < 0 ) {
        alert("像左滑动");
    }
    else if ( Math.abs(Y) > Math.abs(X) && Y > 0) {
        alert("下滑动");
    }
    else if ( Math.abs(Y) > Math.abs(X) && Y < 0 ) {
        alert("上滑动");
    }
    else{
        alert("触摸");
    }
});
        </script>
    </body>
</html>

  

posted @ 2018-06-01 15:07  孤独的DNA  阅读(3458)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3