From d68d5064bc7ea6095581c6a7475e93c3c9295f38 Mon Sep 17 00:00:00 2001 From: scottjehl Date: Wed, 7 Sep 2011 08:15:00 -0400 Subject: [PATCH] finished last-scroll remembering implementation across pages in the history stack (regardless of whether they've been removed from the DOM since last appearance). --- js/jquery.mobile.navigation.js | 35 ++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/js/jquery.mobile.navigation.js b/js/jquery.mobile.navigation.js index 9eded129..adf64fcb 100644 --- a/js/jquery.mobile.navigation.js +++ b/js/jquery.mobile.navigation.js @@ -379,21 +379,32 @@ } } - // Save last scroll distance - $( document ).bind( "beforechangepage", function(){ - var active = $.mobile.urlHistory.getActive(); - if( active ){ - var lastScroll = $( window ).scrollTop(); - - //if the Y location we're scrolling to is less than minScrollBack, let it go for sake of smoothness - if( lastScroll < $.mobile.minScrollBack ){ - lastScroll = $.mobile.defaultHomeScroll; + // Save the last scroll distance per page, before it is hidden + var getLastScroll = (function( lastScrollEnabled ){ + return function(){ + if( !lastScrollEnabled ){ + lastScrollEnabled = true; + return; } - active.lastScroll = lastScroll; - } - }); + lastScrollEnabled = false; + + var active = $.mobile.urlHistory.getActive(); + + if( active ){ + var lastScroll = $( window ).scrollTop(); + + // Set active page's lastScroll prop. + // If the Y location we're scrolling to is less than minScrollBack, let it go. + active.lastScroll = lastScroll < $.mobile.minScrollBack ? $.mobile.defaultHomeScroll : lastScroll; + } + }; + })( true ); + // to get last scroll, we need to get scrolltop before the page change + // using beforechangepage or popstate/hashchange (whichever comes first) + $( document ).bind( "beforechangepage", getLastScroll ); + $( window ).bind( $.support.pushState ? "popstate" : "hashchange", getLastScroll ); //function for transitioning between two existing pages function transitionPages( toPage, fromPage, transition, reverse ) {