1. 程式人生 > >移動端,判斷滑動方向

移動端,判斷滑動方向

直接看程式碼:

var windowHeight = $(window).height();
    $("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) {
            console.log("left to right");
        } else if (Math.abs(X) > Math.abs(Y) && X < 0) {
            console.log("right to left");
        } else if (Math.abs(Y) > Math.abs(X) && Y > 0) {
            console.log("top to bottom");
        } else if (Math.abs(Y) > Math.abs(X) && Y < 0) {
            console.log("bottom to top");
        } else {
            console.log("just touch");
        }
    });

報這個錯 
[Intervention] Ignored attempt to cancel a touchmove event with cancelable=false, for example because scrolling is in progress and cannot be interrupted 
如何解決 

使用css3

 touch-action: none;


touch-action :當你觸控並按住觸控目標時候,禁止或顯示系統預設選單。 
touch-action取值有一下兩種 
none:系統預設選單被禁用 
default:系統預設選單不被禁用