为了账号安全,请及时绑定邮箱和手机立即绑定

为啥document触发房子touchstart外,下次点击触发start,显示start次数加1

标签:
JavaScript

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0">
<style>
*{
margin:0;
padding: 0;
}
.wrapper{
overflow:hidden;
}
.swipArea{
height: 100px;
background-color: green;
text-align: center;
line-height: 100px;
position: absolute;
left: 0;
right: 0;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="swipArea" id="swipArea">swipArea</div>
</div>
<script>
function Swip(el){
this.el = el;
this.init();
}

    Swip.prototype.init = function(){
        var startTouchX = 0,
            startTouchY = 0,
            endTouchX = 0,
            endTouchY = 0;
        var deltaX = 0, deltaY = 0;  
        var direction = "left";
        var d = "";
        var that = this;

        this.el.addEventListener("touchstart",function(evt){
            evt.preventDefault();
            var touches = evt.touches[0];
            startTouchX = touches.pageX;
            startTouchY = touches.pageY;
            // console.log("startTouchX : " + startTouchX)
        },false);
        document.addEventListener("touchmove",function(evt){
            evt.preventDefault();
            var touches = evt.touches[0];
            endTouchX = touches.pageX;
            endTouchY = touches.pageY;
            deltaY = endTouchY - startTouchY;
            if(deltaY > 0) that.el.style.top = deltaY + "px"; 
            // console.log("endTouchX : " + endTouchX + ",endTouchY : " + endTouchY)
        },false);
        document.addEventListener("touchend",function(evt){
            evt.preventDefault();
            document.ontouchmove = null;
            document.ontouchstart = null;
            deltaX = endTouchX - startTouchX;
            deltaY = endTouchY - startTouchY;
            d =  Math.abs(deltaX) > Math.abs(deltaY) ? "h" : "v";
            if(d == "h") direction = deltaX > 0 ? "right" : "left";
            else if(d == "v")  direction = deltaY > 0 ? "down" : "top";
            that.el.style.top = "0";
            // console.log("deltaX : " + deltaX + ",deltaY : " + deltaY)  ;
        },false);
    }
    var swipArea = document.querySelector("#swipArea");
    new Swip(swipArea);
</script>

</body>
</html>

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消