继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

我是蜗牛
关注TA
已关注
手记 1
粉丝 0
获赞 0

<!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>

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP