MediaWiki:Gadget-scroller.js

Z Wikisłownika – wolnego słownika wielojęzycznego

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Naciśnij klawisze Ctrl+F5.
var gadget = window.scroller = {
	version: '1.1'
};

var $content = $( '#mw-content-text' );

// http://stackoverflow.com/a/7557433/5628
function isElementInViewport( el ) {
    var rect = el.getBoundingClientRect();

    return (
        rect.top >= 0 &&
        rect.left >= 0 &&
        rect.bottom <= ( window.innerHeight || document.documentElement.clientHeight ) &&
        rect.right <= ( window.innerWidth || document.documentElement.clientWidth )
    );
}

gadget.scroll = function( selector, action ) {
	var executed = false;
	
	$content.children( selector ).each( function() {
		if ( isElementInViewport( this ) ) {
			var top = this.getBoundingClientRect().top;
			action();
			window.scrollBy( 0, this.getBoundingClientRect().top - top );
			executed = true;
			return false;
		}
	} );
	
	if ( !executed )
		action();
};