Skip to content

Commit aa950ba

Browse files
author
scottjehl
committed
Only call scrollTo if there's a scroll distance we need to go to. If the distance we're jumping to is 20px or less, let it go in for tolerance. Reset toPage height before scrolling, focusing.
1 parent df7ac2f commit aa950ba

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

js/jquery.mobile.navigation.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,19 +386,27 @@
386386
//function for transitioning between two existing pages
387387
function transitionPages( toPage, fromPage, transition, reverse ) {
388388

389-
390389
//get current scroll distance
391390
var currScroll = $window.scrollTop(),
392391
toScroll = toPage.data( "lastScroll" ) || 0;
393392

394-
$.mobile.silentScroll();
395-
393+
//if scrolled down, scroll to top
394+
if( currScroll ){
395+
window.scrollTo( 0, 0 );
396+
}
397+
398+
//if the Y location we're scrolling to is less than 10px, let it go for sake of smoothness
399+
if( toScroll < 20 ){
400+
toScroll = 0;
401+
}
402+
396403
if( fromPage ) {
397404
//set as data for returning to that spot
398405
fromPage
399406
.height( screen.height )
400407
.jqmData( "lastScroll", currScroll )
401408
.jqmData( "lastClicked", $activeClickedLink );
409+
402410
//trigger before show/hide events
403411
fromPage.data( "page" )._trigger( "beforehide", null, { nextPage: toPage } );
404412
}
@@ -416,16 +424,25 @@
416424
promise = th( transition, reverse, toPage, fromPage );
417425

418426
promise.done(function() {
427+
//reset toPage height bac
428+
toPage.height( "" );
429+
419430
//jump to top or prev scroll, sometimes on iOS the page has not rendered yet.
420-
$.mobile.silentScroll( toPage.jqmData( "lastScroll" ) || 0 );
421-
$( document ).one( "silentscroll", function() { reFocus( toPage ); } );
431+
if( toScroll ){
432+
$.mobile.silentScroll( toScroll );
433+
$( document ).one( "silentscroll", function() { reFocus( toPage ); } );
434+
}
435+
else{
436+
reFocus( toPage );
437+
}
422438

423439
//trigger show/hide events
424440
if( fromPage ) {
425441
fromPage.height("").data( "page" )._trigger( "hide", null, { nextPage: toPage } );
426442
}
443+
427444
//trigger pageshow, define prevPage as either fromPage or empty jQuery obj
428-
toPage.height("").data( "page" )._trigger( "show", null, { prevPage: fromPage || $( "" ) } );
445+
toPage.data( "page" )._trigger( "show", null, { prevPage: fromPage || $( "" ) } );
429446

430447
});
431448

0 commit comments

Comments
 (0)