简体   繁体   English

jQuery Mobile-历史记录关闭面板

[英]jQuery mobile - close panel on history back

I have a jQuery mobile panel which slides in from the side, it works great. 我有一个可从侧面滑入的jQuery移动面板,效果很好。 But lets say you have a login page, that redirects to a main page with a panel. 但是可以说您有一个登录页面,该页面重定向到带有面板的主页。 Now if the user opens the panel, and then clicks the back button, he expects the panel to close. 现在,如果用户打开面板,然后单击“后退”按钮,则他希望该面板关闭。 But instead the browser navigates back to the login page. 但是,浏览器会导航回登录页面。

I´ve tried adding something to the url: 我尝试过在网址中添加一些内容:

window.location.hash = "panelOpen";

But that just messes up the jQuery mobile history state pattern. 但这只是弄乱了jQuery移动历史记录状态模式。 I´ve also tried to listen to the navigate event, and prevent it if a panel is open: 我也曾尝试监听Navigation事件,并在面板打开的情况下阻止它:

$(window).on('navigate', function (e, hans) {
     var panels = $('[data-role="panel"].ui-panel-open');
     if (panels&&panels.length>0) {
         e.preventDefault();
         e.stopPropagation();
         $('#' + panels[0].id).panel('close');
           return false;
         }
});

This kind of works, except that the url is changed, and I cannot grab the event that changes the url. 这种工作方式,只是更改了URL,但我无法抓住更改URL的事件。 Furthermore, it also messes up the jQuery mobile history pattern. 此外,它还弄乱了jQuery移动历史记录模式。

So how does people achieve this expected 'app-like' behaviour with a jQuery mobile panel; 因此,人们如何通过jQuery移动面板实现这种预期的“类似于应用程序”的行为; open panel > history back > close panel. 打开面板>历史记录后退>关闭面板。 And thats it. 就是这样。

Thanks alot! 非常感谢!

Updated 更新

Instead of retrieving current URL from jQuery Mobile's history, It is safer to retrieve it from hashchange event event.originalEvent.newURL and then pass it to popstate event to be replaceState() with that URL. 与其从jQuery Mobile的历史记录中检索当前URL相比,更安全的是从hashchange事件event.originalEvent.newURL检索它,然后将其传递给popstate事件,并使用该URL进行replaceState()


Instead of listening to navigate , listen to popstate which fires before. 而不是听navigate ,而是听之前触发的popstate The trick here is manipulate both browser's history and jQuery Mobile's history by replaceState() and reload same page without transition . 这里的技巧是通过replaceState() 操纵浏览器的历史记录和jQuery Mobile的历史记录,并重新加载同一页面而不进行转换

var newUrl;
$(window).on("hashchange", function (e) {
    /* retrieve URL */
    newUrl = e.originalEvent.newURL;
}).on("popstate", function (e) {
    var direction = e.historyState.direction == "back" ? true : false,
        activePanel = $(".ui-panel-open").length > 0 ? true : false,
        url = newUrl,
        title = document.title;
    if (direction && activePanel) {
        $(".ui-panel-open").panel("close");
        $(".ui-header .ui-btn-active").removeClass("ui-btn-active");
        /* reload same page to maintain jQM's history */
        $.mobile.pageContainer.pagecontainer("change", url, {
            allowSamePageTransition: true
        });
        /* replace state to maintain browsers history */
        window.history.replaceState({}, title, url);
        /* prevent navigating into history */
        return false;
    }
});

This part is meant to maintain same transition used previously as transition is set to none when reloading same page. 这部分是为了维护先前使用的相同转换,因为在重新加载同一页面时将transition设置为none

$(document).on("pagebeforechange", function (e, data) {
    if (data.options && data.options.allowSamePageTransition) {
        data.options.transition = "none";
    } else {
        data.options.transition = $.mobile.defaultPageTransition;
    }
});

Demo - Code 演示 - 代码

I am a little bit late on the party, but i had recently the same requirements and i would like to share how i did it. 我参加聚会有点晚了,但是我最近有同样的要求,我想分享一下我的做法。 So, i extended the requirement in the original question to Panels , Popups and Pages : 因此,我将原始问题中的要求扩展到PanelsPopupsPages

...an expected 'app-like' behaviour, history back > close whaterver is open. ...一种预期的“类似于应用程序”的行为,历史回溯>关闭,无论打开了什么。 And thats it. 就是这样。

In .on("panelopen") , .on("popupafteropen") and .on("pagecontainershow") i simply add another entry to the window history, by using the HTML5 API ( https://developer.mozilla.org/en-US/docs/Web/API/History_API ) (I believe there is no need to use the JQM navigate browser quirks for that): .on("panelopen") .on("popupafteropen").on("pagecontainershow")我只是另一项添加到窗口的历史,通过使用HTML5 API( https://developer.mozilla.org/ zh-CN / docs / Web / API / History_API )(我认为不需要为此使用JQM导航浏览器怪癖):

window.history.pushState({}, window.document.title, window.location.href);

After that, i'm using more or less Omar's function to intercept the popstate event: 之后,我使用或多或少的Omar函数来拦截popstate事件:

$(window).on("popstate", function (e) {
  var pageId = $(":mobile-pagecontainer").pagecontainer("getActivePage").prop("id");
  var pageOpen = (pageId != "page-home");
  var panelOpen = $(".ui-panel-open").length > 0;
  var popupOpen = $(".ui-popup-active").length > 0;

  if(pageOpen) {
    $.mobile.pageContainer.pagecontainer("change", "#page-home", {reverse: true});
    return false;
  }
  if(panelOpen) {
    $(".ui-panel-open").panel("close");
    return false;
  }
  if(popupOpen) {
    $(".ui-popup-active .ui-popup").popup("close")
    return false;
  }
});

As you see, the is just only one level to the home-page, but this can be easily extended by using JQM history implementation to get the previous page: 如您所见,仅仅是首页的一个级别,但是可以使用JQM历史记录实现轻松地扩展到上一页:

var activeId = $.mobile.navigate.history.activeIndex;
var jqmHistory = $.mobile.navigate.history.stack; // array of pages

and use pagecontainer to change to the active entry - 1. 并使用pagecontainer更改为活动条目-1。


As last note, this works well also by completely disabling the built-in JQM Ajax navigation system: 最后一点,通过完全禁用内置的JQM Ajax导航系统,这也可以很好地工作:

/* Completely disable navigation for mobile app */
$.mobile.ajaxEnabled = false;
$.mobile.loadingMessage = false;
$.mobile.pushStateEnabled = false;
$.mobile.hashListeningEnabled = false;
$.mobile.changePage.defaults.changeHash = false;
$.mobile.popup.prototype.options.history = false;

(Tested in Browser, on real Android and iOS devices) (在真实的Android和iOS设备上的浏览器中测试)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM