From b1bded6dde31326fc0ecb8aa59de794a24a4c65e Mon Sep 17 00:00:00 2001 From: scottjehl Date: Wed, 10 Nov 2010 19:06:46 -0500 Subject: [PATCH] changed hideBrowserChrome function to "silentScroll". Scrolls to a particular location while disabling scroll event listening. Function accepts a Y positioning argument, or defaults to 0 if undefined. Now page transitions cache their previous scroll position when leaving, and that position will be remembered when revisting that page. This change also includes a fix for webOS to use this new feature and set the previous scroll to the location of the clicked selectmenu (so after using the select, it'll scroll back to its location, even though webOS doesn't support scrolltop) Fixes #355 --- js/jquery.mobile.forms.select.js | 16 +++++++++++----- js/jquery.mobile.js | 21 +++++++++++++++------ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/js/jquery.mobile.forms.select.js b/js/jquery.mobile.forms.select.js index c2a5c642..ed3a7ff8 100644 --- a/js/jquery.mobile.forms.select.js +++ b/js/jquery.mobile.forms.select.js @@ -70,8 +70,7 @@ $.widget( "mobile.selectmenu", $.mobile.widget, { }) .appendTo( listbox ), - menuType, - currScroll; + menuType; //populate menu with options from select element select.find( "option" ).each(function( i ){ @@ -97,8 +96,16 @@ $.widget( "mobile.selectmenu", $.mobile.widget, { function showmenu(){ - var menuHeight = list.outerHeight(); - currScroll = [ $(window).scrollLeft(), $(window).scrollTop() ]; + var menuHeight = list.outerHeight(), + scrollTop = $(window).scrollTop(), + btnOffset = button.offset().top, + screenHeight = window.innerHeight; + + if( scrollTop == 0 && btnOffset > screenHeight ){ + thisPage.one('pagehide',function(){ + $(this).data('lastScroll', btnOffset); + }); + } if( menuHeight > window.innerHeight - 80 || !$.support.scrollTop ){ menuType = "page"; @@ -142,7 +149,6 @@ $.widget( "mobile.selectmenu", $.mobile.widget, { return false; }) .bind("pagehide", function(){ - window.scrollTo(currScroll[0], currScroll[1]); select.focus(); listbox.append( list ).removeAttr('style'); return false; diff --git a/js/jquery.mobile.js b/js/jquery.mobile.js index 5cbe540d..35fa89f9 100644 --- a/js/jquery.mobile.js +++ b/js/jquery.mobile.js @@ -138,12 +138,12 @@ $.mobile.idStringEscape = idStringEscape; // hide address bar - function hideBrowserChrome() { + function silentScroll( ypos ) { // prevent scrollstart and scrollstop events jQuery.event.special.scrollstart.enabled = false; setTimeout(function() { - window.scrollTo( 0, 0 ); - },0); + window.scrollTo( 0, ypos || 0 ); + },20); setTimeout(function() { jQuery.event.special.scrollstart.enabled = true; }, 150 ); @@ -354,6 +354,12 @@ //kill the keyboard jQuery( window.document.activeElement ).blur(); + //get current scroll distance + var currScroll = $window.scrollTop(); + + //set as data for returning to that spot + from.data('lastScroll', currScroll); + //trigger before show/hide events from.data("page")._trigger("beforehide", {nextPage: to}); to.data("page")._trigger("beforeshow", {prevPage: from}); @@ -374,9 +380,12 @@ if( duplicateCachedPage != null ){ duplicateCachedPage.remove(); } + + //jump to top or prev scroll, if set + silentScroll( to.data( 'lastScroll' ) ); } - if(transition && (transition !== 'none')){ + if(transition && (transition !== 'none')){ $pageContainer.addClass('ui-mobile-viewport-transitioning'); // animate in / out from.addClass( transition + " out " + ( back ? "reverse" : "" ) ); @@ -604,7 +613,7 @@ jQuery.extend({ pageLoading: pageLoading, changePage: changePage, - hideBrowserChrome: hideBrowserChrome + silentScroll: silentScroll }); //dom-ready @@ -633,6 +642,6 @@ $html.removeClass('ui-mobile-rendering'); }); - $window.load(hideBrowserChrome); + $window.load(silentScroll); })( jQuery, this );