javascript - 如何检测 html web 应用程序中的滑动原点位置?

标签 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% 开始,那么代码应该触发。

有人知道怎么做吗?

谢谢

最佳答案

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

关于javascript - 如何检测 html web 应用程序中的滑动原点位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29066879/

相关文章:

javascript - 如何在不同的范围内重用变量?

javascript - 模块化JS : global functionality on 3 sections (checkboxes and radios) & Sholud I apply "rendering" in my code?

javascript - 如何在 w2ui 中使布局小部件扩展到完整高度?

html - 如何让 CSS 三 Angular 形显示在一行而不是垂直

javascript - 上传文件夹和文件

javascript - 在加载指令模板后执行 Javascript

javascript - jQuery scrollTop() 问题

c# - FullCalendar 提前一小时

javascript - JQuery Mobile Listview 元素消失

javascript - 如何让 Ace Editor 以与 div 相同的方式扩展和填充表格单元格?