diff --git a/js/jquery.mobile.init.js b/js/jquery.mobile.init.js index 1a66e0bd..dbb0d47a 100644 --- a/js/jquery.mobile.init.js +++ b/js/jquery.mobile.init.js @@ -83,9 +83,14 @@ // define first page in dom case one backs out to the directory root (not always the first page visited, but defined as fallback) $.mobile.firstPage = $pages.first(); + // define page container $.mobile.pageContainer = $pages.first().parent().addClass( "ui-mobile-viewport" ); + // alert listeners that the pagecontainer has been determined for binding + // to events triggered on it + $window.trigger( "pagecontainercreate" ); + // cue page loading message $.mobile.showPageLoadingMsg(); diff --git a/js/jquery.mobile.navigation.js b/js/jquery.mobile.navigation.js index 4fa57741..1c6335f2 100755 --- a/js/jquery.mobile.navigation.js +++ b/js/jquery.mobile.navigation.js @@ -399,54 +399,81 @@ } // Save the last scroll distance per page, before it is hidden - var setLastScroll = function(){ - if( setLastScrollEnabled ) { - return; - } + var setLastScrollEnabled = true, getScrollElem, setLastScroll; - var active = $.mobile.urlHistory.getActive(), - scrollElem = $window, - touchOverflow = $.support.touchOverflow && $.mobile.touchOverflowEnabled, - activePage; + getScrollElem = function() { + var scrollElem = $window, activePage, + touchOverflow = $.support.touchOverflow && $.mobile.touchOverflowEnabled; if( touchOverflow ){ activePage = $( ".ui-page-active" ); scrollElem = activePage.is( ".ui-native-fixed" ) ? activePage.find( ".ui-content" ) : activePage; } - if( active ){ + return scrollElem; + }; + + setLastScroll = function( scrollElem ) { + // this barrier prevents setting the scroll value based on the browser + // scrolling the window based on a hashchange + if( !setLastScrollEnabled ) { + return; + } + + var active = $.mobile.urlHistory.getActive(); + + if( active ) { var lastScroll = scrollElem.scrollTop(); // Set active page's lastScroll prop. - // If the Y location we're scrolling to is less than minScrollBack, let it go. + // If the location we're scrolling to is less than minScrollBack, let it go. active.lastScroll = lastScroll < $.mobile.minScrollBack ? $.mobile.defaultHomeScroll : lastScroll; } }; - var setLastScrollEnabled = false; - // bind to scrollstop to gather scroll position. The delay allows for the hashchange // event to fire and disable scroll recording in the case where the browser scrolls // to the hash targets location (sometimes the top of the page). once pagechange fires // getLastScroll is again permitted to operate - $( window ).bind( "scrollstop", function() { - setTimeout(setLastScroll, 100); - }); + var firstScrollElem = getScrollElem(), handleSetLastScroll; - $( window ).bind( $.support.pushState ? "popstate" : "hashchange", function() { - setLastScrollEnabled = true; - }); + handleSetLastScroll = function( opts ) { + setTimeout(setLastScroll, 100, opts.data.scrollElem ); + }; -<<<<<<< HEAD - // to get last scroll, we need to get scrolltop before the page change - // using pagebeforechange or popstate/hashchange (whichever comes first) - $( document ).bind( "pagebeforechange", getLastScroll ); - $( window ).bind( $.support.pushState ? "popstate" : "hashchange", getLastScroll ); -======= - $( window ).bind( "pagechange", function() { + // disable an scroll setting when a hashchange has been fired, this only works + // because the recording of the scroll position is delayed for 100ms after + // the browser might have changed the position because of the hashchange + $window.bind( $.support.pushState ? "popstate" : "hashchange", function() { setLastScrollEnabled = false; }); ->>>>>>> use scrolltop to store scroll position + + // handle initial hashchange from chrome :( + $window.one( $.support.pushState ? "popstate" : "hashchange", function() { + setLastScrollEnabled = true; + }); + + // wait until the mobile page container has been determined to bind to pagechange + $window.one( "pagecontainercreate", function(){ + // once the page has changed, re-enable the scroll recording + $.mobile.pageContainer.bind( "pagechange", function() { + var scrollElem = getScrollElem(); + + setLastScrollEnabled = true; + + // remove any binding that previously existed on the get scroll + // which may or may not be different than the scroll element determined for + // this page previously + scrollElem.unbind( "scrollstop", handleSetLastScroll); + + // determine and bind to the current scoll element which may be the window + // or in the case of touch overflow the element with touch overflow + scrollElem.bind( "scrollstop", { scrollElem: scrollElem }, handleSetLastScroll); + }); + }); + + // bind to scrollstop for the first page as "pagechange" won't befired in that case + firstScrollElem.bind( "scrollstop", { scrollElem: firstScrollElem }, handleSetLastScroll); // Make the iOS clock quick-scroll work again if we're using native overflow scrolling /*