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

前端 未结 6 1262
一生所求
一生所求 2020-12-06 01:02

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 jQ

相关标签:
6条回答
  • 2020-12-06 01:38
      $( 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);
            }
        }
      }
    
    0 讨论(0)
  • 2020-12-06 01:42

    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

    0 讨论(0)
  • 2020-12-06 01:47

    Solution to prevent scrool to top is:

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

    0 讨论(0)
  • 2020-12-06 01:49

    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.

    0 讨论(0)
  • 2020-12-06 01:52

    try to use scrollstart to detect window scroll event in jquery mobile, in case you need :)

    0 讨论(0)
  • 2020-12-06 01:57

    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:

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

    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.

    0 讨论(0)
提交回复
热议问题