MediaWiki:Gadget-updateURLOnScroll.js
OBS: Efter du har publicerat sidan kan du behöva tömma din webbläsares cache för att se ändringarna.
- Firefox / Safari: Håll ned Skift och klicka på Uppdatera sidan eller tryck Ctrl-F5 eller Ctrl-R (⌘-R på Mac)
- Google Chrome: Tryck Ctrl-Skift-R (⌘-Skift-R på Mac)
- Internet Explorer / Edge: Håll ned Ctrl och klicka på Uppdatera eller tryck Ctrl-F5
- Opera: Tryck Ctrl-F5.
$( function() {
'use strict';
function isAnyPartOfElementInViewport( el ) {
var rect = el.getBoundingClientRect();
var windowHeight = ( window.innerHeight || document.documentElement.clientHeight );
var windowWidth = ( window.innerWidth || document.documentElement.clientWidth );
var vertInView = ( rect.top <= windowHeight ) && ( ( rect.top + rect.height ) >= 0);
var horInView = ( rect.left <= windowWidth ) && ( ( rect.left + rect.width ) >= 0 );
return ( vertInView && horInView );
}
var headings;
var headingids = [];
var encodedheadingids;
var prefixedencodedheadingids;
var oldhash;
if ( !location.hash ) {
headings = $( '.mw-headline' );
if ( headings.length ) {
headings.each( function( i, e ) {
headingids.push( e.id );
} );
encodedheadingids = headingids.map( function( v ) {
return encodeURIComponent( v );
} );
prefixedencodedheadingids = encodedheadingids.map( function( v ) {
return '#' + v;
} );
$( document ).scroll( function() {
var headingsInViewport = headings.filter( function( i, e ) {
return isAnyPartOfElementInViewport( e );
} );
var firstHeadingInViewport;
if ( headingsInViewport.length ) {
firstHeadingInViewport = headingsInViewport[ 0 ];
if ( firstHeadingInViewport.id !== oldhash ) {
history.replaceState( undefined, undefined, '#' + encodeURIComponent( firstHeadingInViewport.id ) );
oldhash = firstHeadingInViewport.id;
}
}
if ( $( document ).scrollTop() === 0 ) {
if ( prefixedencodedheadingids.includes( location.hash ) ) {
history.replaceState( undefined, undefined, location.pathname + location.search );
oldhash = null;
}
}
} );
}
}
} );