スワイプ左/右にWebページを追加しますが、デフォルトのスワイプアップ/ダウンを使用します

StackOverflow https://stackoverflow.com/questions/5304340

  •  24-10-2019
  •  | 
  •  

質問

iOSとデスクトップで表示されるWebページのスワイプジェスチャーを検出するためにPadiliciousを使用しています。私のサイトの前後のページに向けて、左/右をスワイプするのに最適です。ただし、上下にスワイプするときにiPhone/iPadのデフォルトの動作をオーバーライドするようです。ページをスクロールするためにアップ/ダウンのスワイプが必要です。コードを無視するだけでスワイプを無視するだけでは機能しないようです。

私が行ったPadiliciousコードのセクション

function processingRoutine() {
    var swipedElement = document.getElementById(triggerElementID);
    if ( swipeDirection == 'left' ) {
        document.location = document.getElementById('nextPage').href;
    } else if ( swipeDirection == 'right' ) {
        document.location = document.getElementById('prevPage').href;
    } else if ( swipeDirection == 'up' ) {
        return;
    } else if ( swipeDirection == 'down' ) {
        return;
    }


}
役に立ちましたか?

解決

削除する event.preventDefault(); すべての機能から。関数で processingRoutine() {} 追加 event.preventDefault(); あなたが望むもののために。

function processingRoutine() {
    var swipedElement = document.getElementById(triggerElementID);
    if ( swipeDirection == 'left' ) {
        // REPLACE WITH YOUR ROUTINES
        //swipedElement.style.backgroundColor = 'orange';
        event.preventDefault();
    } else if ( swipeDirection == 'right' ) {
        // REPLACE WITH YOUR ROUTINES
        //swipedElement.style.backgroundColor = 'green';
        event.preventDefault();
    } else if ( swipeDirection == 'up' ) {
        // REPLACE WITH YOUR ROUTINES
        //swipedElement.style.backgroundColor = 'maroon';
    } else if ( swipeDirection == 'down' ) {
        // REPLACE WITH YOUR ROUTINES
        //swipedElement.style.backgroundColor = 'purple';
    }
}

他のヒント

私はパディリシャスに精通していませんが、チェックして確認してください ontouchmove="BlockMove(event);" どこにでも設定されているため、ページが説明するようにスクロールするのを防ぎます。垂直スクロールを維持して水平方向にスワイプする方法がわかりません。

編集: :それ以来、「ネイティブ」感情のiOS Webアプリを実行するための非常に役立つ概要を見つけましたが、それはあなたが探しているものではないかもしれませんが、あなたの問題への別のアプローチの道を提供することができます。見てみな: http://matt.might.net/articles/how-to-native-iphone-ipad-apps-in-javascript/

Padiliciousは、すべての場合においてデフォルトを妨げているようです。すべての場合において、event.preventdefault()の呼び出しを参照してください。

function touchStart(event,passedName) {
  // disable the standard ability to select the touched object
  event.preventDefault();

上下のケースでPreventDefault()を呼び出さないように、開始、停止、...ハンドラーを変更する必要があります。

私はスクリプトを変更しました、この作業:

    // TOUCH-EVENTS SINGLE-FINGER SWIPE-SENSING JAVASCRIPT
// Courtesy of PADILICIOUS.COM and MACOSXAUTOMATION.COM

// this script can be used with one or more page elements to perform actions based on them being swiped with a single finger

var triggerElementID = null; // this variable is used to identity the triggering element
var fingerCount = 0;
var startX = 0;
var startY = 0;
var curX = 0;
var curY = 0;
var deltaX = 0;
var deltaY = 0;
var horzDiff = 0;
var vertDiff = 0;
var minLength = 72; // the shortest distance the user may swipe
var swipeLength = 0;
var swipeAngle = null;
var swipeDirection = null;

// The 4 Touch Event Handlers

// NOTE: the touchStart handler should also receive the ID of the triggering element
// make sure its ID is passed in the event call placed in the element declaration, like:
// <div id="picture-frame" ontouchstart="touchStart(event,'picture-frame');"  ontouchend="touchEnd(event);" ontouchmove="touchMove(event);" ontouchcancel="touchCancel(event);">

function touchStart(event,passedName) {
    // disable the standard ability to select the touched object
    //event.preventDefault();
    // get the total number of fingers touching the screen
    fingerCount = event.touches.length;
    // since we're looking for a swipe (single finger) and not a gesture (multiple fingers),
    // check that only one finger was used
    if ( fingerCount == 1 ) {
        // get the coordinates of the touch
        startX = event.touches[0].pageX;
        startY = event.touches[0].pageY;
        // store the triggering element ID
        triggerElementID = passedName;
    } else {
        // more than one finger touched so cancel
        touchCancel(event);
    }
}

function touchMove(event) {
    //event.preventDefault();
    if ( event.touches.length == 1 ) {
        curX = event.touches[0].pageX;
        curY = event.touches[0].pageY;
    } else {
        touchCancel(event);
    }
}

function touchEnd(event) {
    //event.preventDefault();
    // check to see if more than one finger was used and that there is an ending coordinate
    if ( fingerCount == 1 && curX != 0 ) {
        // use the Distance Formula to determine the length of the swipe
        swipeLength = Math.round(Math.sqrt(Math.pow(curX - startX,2) + Math.pow(curY - startY,2)));
        // if the user swiped more than the minimum length, perform the appropriate action
        if ( swipeLength >= minLength ) {
            caluculateAngle();
            determineSwipeDirection();
            processingRoutine();
            touchCancel(event); // reset the variables
        } else {
            touchCancel(event);
        }   
    } else {
        touchCancel(event);
    }
}

function touchCancel(event) {
    // reset the variables back to default values
    fingerCount = 0;
    startX = 0;
    startY = 0;
    curX = 0;
    curY = 0;
    deltaX = 0;
    deltaY = 0;
    horzDiff = 0;
    vertDiff = 0;
    swipeLength = 0;
    swipeAngle = null;
    swipeDirection = null;
    triggerElementID = null;
}

function caluculateAngle() {
    var X = startX-curX;
    var Y = curY-startY;
    var Z = Math.round(Math.sqrt(Math.pow(X,2)+Math.pow(Y,2))); //the distance - rounded - in pixels
    var r = Math.atan2(Y,X); //angle in radians (Cartesian system)
    swipeAngle = Math.round(r*180/Math.PI); //angle in degrees
    if ( swipeAngle < 0 ) { swipeAngle =  360 - Math.abs(swipeAngle); }
}

function determineSwipeDirection() {
    if ( (swipeAngle <= 45) && (swipeAngle >= 0) ) {
        swipeDirection = 'left';
    } else if ( (swipeAngle <= 360) && (swipeAngle >= 315) ) {
        swipeDirection = 'left';
    } else if ( (swipeAngle >= 135) && (swipeAngle <= 225) ) {
        swipeDirection = 'right';
    }

    /* else if ( (swipeAngle > 45) && (swipeAngle < 135) ) {
        swipeDirection = 'down';
    } else {
        swipeDirection = 'up';
    }*/
}

function processingRoutine() {
    var swipedElement = document.getElementById(triggerElementID);
    if ( swipeDirection == 'left' ) {
        // REPLACE WITH YOUR ROUTINES
        event.preventDefault();
        swipedElement.style.backgroundColor = 'orange';
    } else if ( swipeDirection == 'right' ) {
        // REPLACE WITH YOUR ROUTINES
        event.preventDefault();
        swipedElement.style.backgroundColor = 'green';
    } 

    /*else if ( swipeDirection == 'up' ) {
        // REPLACE WITH YOUR ROUTINES
        swipedElement.style.backgroundColor = 'maroon';
    } else if ( swipeDirection == 'down' ) {
        // REPLACE WITH YOUR ROUTINES
        swipedElement.style.backgroundColor = 'purple';
    }*/
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top