nav: fix initial positioning on content pages #476

This commit is contained in:
Sören Weber 2023-02-12 14:10:55 +01:00
parent a40e3685d4
commit 09e7aebcd8
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
2 changed files with 16 additions and 14 deletions

View file

@ -65,7 +65,7 @@ function triggerSearch(){
// with normal pages, this is handled by the 'pagehide' event, but this // with normal pages, this is handled by the 'pagehide' event, but this
// doesn't fire in case of pushState, so we have to do the same thing // doesn't fire in case of pushState, so we have to do the same thing
// here, too // here, too
state.contentScrollTop = elc.scrollTop; state.contentScrollTop = +elc.scrollTop;
window.history.pushState( state, '', url ); window.history.pushState( state, '', url );
} }
} }
@ -239,9 +239,9 @@ function searchDetail( value ) {
// by a browser history operation, it simply does nothing // by a browser history operation, it simply does nothing
var state = window.history.state || {}; var state = window.history.state || {};
state = Object.assign( {}, ( typeof state === 'object' ) ? state : {} ); state = Object.assign( {}, ( typeof state === 'object' ) ? state : {} );
if( state.contentScrollTop ){ if( state.hasOwnProperty( 'contentScrollTop' ) ){
window.setTimeout( function(){ window.setTimeout( function(){
elc.scrollTop = state.contentScrollTop; elc.scrollTop = +state.contentScrollTop;
}, 10 ); }, 10 );
return; return;
} }

View file

@ -819,7 +819,7 @@ function initScrollPositionSaver(){
function savePosition( event ){ function savePosition( event ){
var state = window.history.state || {}; var state = window.history.state || {};
state = Object.assign( {}, ( typeof state === 'object' ) ? state : {} ); state = Object.assign( {}, ( typeof state === 'object' ) ? state : {} );
state.contentScrollTop = elc.scrollTop; state.contentScrollTop = +elc.scrollTop;
window.history.replaceState( state, '', window.location ); window.history.replaceState( state, '', window.location );
}; };
window.addEventListener( 'pagehide', savePosition ); window.addEventListener( 'pagehide', savePosition );
@ -846,15 +846,16 @@ function scrollToPositions() {
var state = window.history.state || {}; var state = window.history.state || {};
state = ( typeof state === 'object') ? state : {}; state = ( typeof state === 'object') ? state : {};
if( state.contentScrollTop !== undefined ){ if( state.hasOwnProperty( 'contentScrollTop' ) ){
window.setTimeout( function(){ window.setTimeout( function(){
elc.scrollTop = state.contentScrollTop; elc.scrollTop = +state.contentScrollTop;
}, 10 ); }, 10 );
return; return;
} }
var searchValue = sessionStorage.getItem( baseUriFull+'search-value' ); var search = sessionStorage.getItem( baseUriFull+'search-value' );
var found = elementContains( searchValue, elc ); if( search && search.length ){
var found = elementContains( search, elc );
var searchedElem = found.length && found[ 0 ]; var searchedElem = found.length && found[ 0 ];
if( searchedElem ){ if( searchedElem ){
searchedElem.scrollIntoView( true ); searchedElem.scrollIntoView( true );
@ -862,6 +863,7 @@ function scrollToPositions() {
if( scrolledY ){ if( scrolledY ){
window.scroll( 0, scrolledY - 125 ); window.scroll( 0, scrolledY - 125 );
} }
}
return; return;
} }