如何在HTML网页应用中检测滑动起源位置?

时间:2015-03-15 22:20:03

标签: javascript jquery html jquery-mobile swipe

在我的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%,我向右滑动,这将触发。我想要它,这样如果我向右滑动,我的手指从屏幕x轴的< = 20%开始,那么代码应该触发。

有谁知道怎么做?

由于

2 个答案:

答案 0 :(得分:1)

在滑动事件处理程序中,使用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" );
    }
});

答案 1 :(得分:0)

虽然我从未使用过它 - 试试这个库:http://interactjs.io/。看起来很有前途且记录良好。