Smart scrolling

Prevent scrolling issues when page is resized by loading images

Versione datata 23/06/2016. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name        Smart scrolling
// @namespace   [email protected]
// @description Prevent scrolling issues when page is resized by loading images
// @include     *
// @version     1.3
// @grant       none
// @run-at      document-start
// ==/UserScript==

(function() {
	var hash = location.hash.replace("#", "");
	var hashListener = function() {
		removeEventListener("wheel", hashListener);
		hash = "";
	};
	addEventListener("wheel", hashListener);
	var update = function() {
		var scrollY = 0, ready = document.readyState == "complete", elem;
		if(hash) {
			elem = document.getElementById(hash) || document.querySelector("a[name='"+hash+"']");
			if(elem) scrollY = Math.round(elem.getBoundingClientRect().top);
		} else {
			var images = [].slice.call(document.getElementsByTagName("IMG")),
				overflows = ["auto", "scroll"], data, rect, height;
			for(var i = 0; i < images.length; i++) {
				if(!images[i].zssData) images[i].zssData = {};
				data = images[i].zssData;
				if(ready) delete images[i].zssData;
				if(data.complete) continue;
				rect   = images[i].getBoundingClientRect();
				height = Math.max(data.height || 0, Math.round(rect.bottom - rect.top));
				if(isNaN(data.height)) {
					if(images[i].complete || !images[i].offsetParent || getComputedStyle(images[i]).position == "fixed")
						data.complete = true;
					else
						data.height = height;
				} else if(data.height != height) {
					data.complete = true;
					if(rect.top >= 0) continue;
					elem = images[i];
					while(true) {
						elem = elem.parentNode;
						if(elem == document.body) {
							scrollY += height - data.height;
							break;
						} else if(overflows.includes(getComputedStyle(elem).overflow)) {
							elem.scrollTop += height - data.height;
							break;
						}
					}
				}
			}
		}
		if(scrollY !== 0) scrollBy(0, scrollY);
		if(!ready) requestAnimationFrame(update);
	};
	update();
})();