JS移动端判断上拉和下滑

touchstart有touches属性,touchend有changedTouches属性,两个属性中分别有pageY、pageX信息。
    //滑动处理
    var startX, startY;
    document.addEventListener('touchstart',function (ev) {
        startX = ev.touches[0].pageX;
        startY = ev.touches[0].pageY;
    }, false);

    document.addEventListener('touchend',function (ev) {
        var endX, endY;
        endX = ev.changedTouches[0].pageX;
        endY = ev.changedTouches[0].pageY;
        var direction = GetSlideDirection(startX, startY, endX, endY);
        switch(direction) {
            case 0:
                    alert("无操作");
                break;
            case 1:
                // 向上
                alert("up");
                break;
            case 2:
                // 向下
                alert("down");
                break;

            default:
        }
      }, false);

    function GetSlideDirection(startX, startY, endX, endY) {
        var dy = startY - endY;
        //var dx = endX - startX;
        var result = 0;
        if(dy>0) {//向上滑动
            result=1;
        }else if(dy<0){//向下滑动
            result=2;
        }
        else
        {
            result=0;
        }
        return result;
     }

你可能感兴趣的:(JS移动端判断上拉和下滑)