简体   繁体   中英

jQuery Mobile prevent scroll-to-top before page transition?

When I click on a list-item, to go to another page, the current page jumps to the top of the screen before transitioning to the next page.

This problem occured in jQM 1.2, and is still not fixed in the newly released 1.3 version.

Does anybody know how to prevent the scroll-to-top, and remember the scroll position when using the back button?

This annoying behaviour is unacceptable, and breaks the whole app experience.

I'm using it as a webapp, on an iPhone 4S, with iOS 6.1.2.

Before I describe your available solutions you need to understand, this is not an error nor is there a perfect solution. The issue is that to animate the transition to another page the viewport has to be at the top of the page so that the current page and the page transitioning in are vertically lined-up.

If you were half-way down a long list on one page (say 1000px) and the page you are transferring to is only a few hundred pixels tall then the current page would animate off the screen properly but the new page would not be visible as it would be above the viewport.

There are 2 viable solutions:

1. iScroll and its jQuery Mobile derivate iScrollview

iScroll homepage: http://cubiq.org/iscroll-4

iScrollview homepage: https://github.com/watusi/jquery-mobile-iscrollview

iScroll is a javascript that can scroll content in a window within a web browser with very similar behaviour to native scrolling on mobile devices such as iPhone and Android. This means you can scroll a window within the browser using native-like scrollbars and physics.

That is also a solution for our current problem. Because of iScroll implementation pages will occupy 100% of page height, no matter how far listview is scrolled. This is also a reason why on return listview will still stay at a same position.

Of course in case you want to implement this solution you should pick iScrollview implementation. You would still be able to implement iScroll, but it would take you much more time.

2. Silent scroll

Official documentation: http://jquerymobile.com/demos/1.1.0-rc.1/docs/api/methods.html

This jQuery Mobile functionality is also the same reason why we have this problem at the first place. Before a page transition is triggered original page is silently scrolled to the top.

In our case, when listview is selected, its position must be remembered ( here you will find solutions of data/parameteres storing during the page transition, just search for the chapter: Data/Parameters manipulation between page transitions ) before page is changed. In that case, when we return to the previous page we could use pagebefpreshow event to silently scroll to the bottom before page is shown.

//scroll to Y 100px
$.mobile.silentScroll(100);

And here's a working example of silent scroll: http://jsfiddle.net/Gajotres/5zZzz/

More info

If you want to find out more about this topic take a look at this article , you will also find working examples.

I was able to fix this (for iOS) in the following way:

  1. Add a extra container div for the scrolling part. Usually surrounding the scrolling part of your page. In my case right after the header and before the footer code.

  2. Add the following CSS:

    \n.extracontainer { \n  width: 100%;\n  height: 100%;\n  position: absolute;\n  top: 0;\n  right: 0;\n  bottom: 0;\n  left: 0;\n  margin: 0;\n  padding: 0;\n  overflow: auto;\n  overflow-y: scroll;\n  -webkit-overflow-scrolling: touch; \n} \n

Some of the CSS might be extra but in my case it was to avoid any issues with some other styles that I have using negative margins, paddings, etc.

Also make sure to have the -webkit-overflow-scrolling: touch; to have smooth scrolling.

I hope this helps.

  $( document ).on( "mobileinit", function() {
        var silentScroll = $.mobile.silentScroll;
          $.mobile.silentScroll = function( ypos ) {
        if ( $.type( ypos ) !== "number" ) {
            // FIX : prevent auto scroll to top after page load
            return;
        } else {
            silentScroll.apply(this, arguments);
        }
    }
  }

For Jquery Mobile 1.4.5 i fixed that by changing this line in jquery.mobile-1.4.5.min.js:

a.mobile.hideUrlBar&&g.load(a.mobile.silentScroll)

to this:

a.mobile.hideUrlBar

Solution to prevent scrool to top is:

body onload="$.mobile.silentScroll(window.scrollY);"

尝试使用scrollstart检测 jquery mobile 中的窗口滚动事件,以防万一:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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