如何检测 html 网络应用中的滑动原点位置?

How to detect swipe origin location in html web apps?

在我的 jquery 移动应用程序中,我有来自他们网站的代码,可以检测滑动以便使用滑动打开面板。这是代码:

function swipePanel(pageId, leftPanelId) {
    $( document ).on( "pageinit", pageId, function() {
        $( document ).on( "swipeleft swiperight", pageId, function( e ) {
            // We check if there is no open panel on the page because otherwise
            // a swipe to close the left panel would also open the right panel (and v.v.).
            // We do this by checking the data that the framework stores on the page element (panel: open).
            if ( $.mobile.activePage.jqmData( "panel" ) !== "open" ) {
                if ( e.type === "swiperight" ) {
                    $( leftPanelId ).panel( "open" );
                }
            }
        });
    });
}

我去掉了一点(左滑检测因为我只需要右滑)。

问题是,如果我从任何地方向右滑动,这段代码就会触发,比如如果我的手指超过屏幕 x 轴的 75%,然后我向右滑动,就会触发。我想要这样,如果我向右滑动,并且我的手指从 <=20% 的屏幕 x 轴开始,那么代码应该触发。

有人知道怎么做吗?

谢谢

虽然我从未使用过它 - 试试这个库:http://interactjs.io/。看起来很有希望并且有据可查。

在滑动事件处理程序中,使用 swipestart.coords[0] 获取滑动开始位置的 x 坐标。然后计算页面宽度的20%:

$( document ).on( "swiperight", "#page1", function( e ) {
    var startX = e.swipestart.coords[0];
    var totalWidth = $(window).width();
    if (startX < totalWidth/5){
        $("#thePanel").panel( "open" );
    }
});