jQuery mobile panel open scrolls page to the top, how to change this?

前端 未结 4 1568
误落风尘
误落风尘 2021-01-07 01:43

So I\'m developing a jQuery mobile phonegap app and I noticed that every time I open the panel the main page scrolls to the top automatically. I want the page to remain at t

4条回答
  •  不知归路
    2021-01-07 02:15

    Here is an option I wrote. It takes the approach of storing the scroll position of the page before a panel opens and restoring it when the panels closes. I have some really long panels and this worked for me. This has been written generically so it wires up to all panels. You can change the selectors to wire up against just certain panels.

    /*******************************************************************************
     * When a panel opens, it makes you lose your place whithin the main page.
     * These functions fix that by tracking where you are in the page before the
     * panels opens and restoring that position when the panel closes     
     ******************************************************************************/
    
    $(document).on("panelbeforeopen", "[data-role=page]", function(e) {
        /* if page we're leaving is a regular page, store the scroll position */
        if ($.mobile.activePage.attr("data-role")=="page" && $("body").scrollTop()!=0) 
        {
             window._scrollTop = $("body").scrollTop();
        }
    });
    
    $(document).on("panelopen", "[data-role=panel]", function(e) {
       /* when the panel finishes opening scroll to the top
       $.mobile.silentScroll(0);
    });
    
    $(document).on("panelclose", "[data-role=panel]", function(e) {
       /* when the panel closes, restore the last stored scroll position */ 
       $.mobile.silentScroll(window._scrollTop);
    });

提交回复
热议问题