jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/344795 )
Change subject: Fix TypeError: undefined is not an object (evaluating '$(href).offset().top') ...................................................................... Fix TypeError: undefined is not an object (evaluating '$(href).offset().top') Bug: T157532 Change-Id: I1d94c41656b60169f5cedcf8b067603539a72943 --- M js/fixed-header-scroll-fix.js 1 file changed, 29 insertions(+), 15 deletions(-) Approvals: Paladox: Looks good to me, approved jenkins-bot: Verified diff --git a/js/fixed-header-scroll-fix.js b/js/fixed-header-scroll-fix.js index d2fe16d..1ad4483 100644 --- a/js/fixed-header-scroll-fix.js +++ b/js/fixed-header-scroll-fix.js @@ -1,17 +1,31 @@ -/* Add offset when scrolling to an anchor from a table of contents link */ -var href = $( this ).attr( 'href' ), - dest = ( $( href ).offset().top - - ( $( 'div.vectorMenu#usermenu' ).height() + 10 ) ), - d = ( $( ':target' ).offset().top - - ( $( 'div.vectorMenu#usermenu' ).height() + 10 ) ); +jQuery( document ).ready( function( $ ) { + /* Add offset when scrolling to an anchor from a table of contents link */ + var dest, + headerHeight = $( 'div.vectorMenu#usermenu' ).height() + 10, + // Split hash + hash; -$( '.toc ul a[href^=#]' ).on( 'click', function( e ) { - window.history.pushState( {}, '', $( this ).prop( 'href' ) ); - $( 'html, body' ).scrollTop( dest ); - e.preventDefault(); + $( '.toc ul a[href*="#"]' ).click( function( e ) { + try { + if ( this && this.href !== '' ) { + // Split location and hash + hash = this.href.match( /[#].*/g )[ 0 ], + + // Don't reload page if already at same location as last clicked + $( 'html, body' ).animate( { + scrollTop: $( hash ).offset().top - 41 + }, 200 ); + } + e.preventDefault(); + return false; + } catch ( e ) { } + } ); + + /* Add offset when scrolling to an anchor present at page load time */ + if ( $( ':target' ).length > 0 ) { + dest = $( this ).offset().top - headerHeight; + $( 'html, body' ).animate( { + scrollTop: dest // also use scrollTop to animate scrolling + } ); + } } ); - -/* Add offset when scrolling to an anchor present at page load time */ -if ( $( ':target' ).length > 0 ) { - $( 'html, body' ).scrollTop( d ); -} -- To view, visit https://gerrit.wikimedia.org/r/344795 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I1d94c41656b60169f5cedcf8b067603539a72943 Gerrit-PatchSet: 7 Gerrit-Project: mediawiki/skins/Metrolook Gerrit-Branch: master Gerrit-Owner: Paladox <thomasmulhall...@yahoo.com> Gerrit-Reviewer: Paladox <thomasmulhall...@yahoo.com> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits