html5滑动事件代码

$(".header").on("touchstart", function(e) {
    // 判断默认行为是否可以被禁用
    if (e.cancelable) {
        // 判断默认行为是否已经被禁用
        if (!e.defaultPrevented) {
            e.preventDefault();
        }
    }   
    startX = e.originalEvent.changedTouches[0].pageX,
    startY = e.originalEvent.changedTouches[0].pageY;
});
$(".header").on("touchend", function(e) {         
    // 判断默认行为是否可以被禁用
    if (e.cancelable) {
        // 判断默认行为是否已经被禁用
        if (!e.defaultPrevented) {
            e.preventDefault();
        }
    }               
    moveEndX = e.originalEvent.changedTouches[0].pageX,
    moveEndY = e.originalEvent.changedTouches[0].pageY,
    X = moveEndX - startX,
    Y = moveEndY - startY;
    //左滑
    if ( X > 0 ) {
        if(X>150){
            console.log('左滑',X);
        }
    }
    //右滑
    else if ( X < 0 ) {
        if(X<-150){
            console.log('右滑',X);
        }  
    }
    //下滑
    else if ( Y > 0) {
        console.log('下滑', Y);    
    }
    //上滑
    else if ( Y < 0 ) {
        console.log('上滑', Y);    
    }
    //单击
    else{
        console.log('单击');    
    }
});

  

posted @ 2018-07-25 14:32  scott_j  阅读(1427)  评论(0编辑  收藏  举报