Smart scrolling

Prevent scrolling issues when page is resized by loading images

Від 06.06.2016. Дивіться остання версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Smart scrolling
// @namespace   [email protected]
// @description Prevent scrolling issues when page is resized by loading images
// @include     *
// @version     1.0
// @grant       none
// ==/UserScript==

(function() {
	var images = document.getElementsByTagName('IMG');
	var imagesHeight = [];
	var imagesParent = [];
	var update = function() {
		var style, rect, height;
		for(var i = 0; i < images.length; i++) {
			style = getComputedStyle(images[i]);
			if(style.display == 'none' || style.position == 'fixed') continue;
			rect = images[i].getBoundingClientRect();
			height = Math.round(rect.bottom - rect.top);
			if(!imagesParent[i]) {
				imagesParent[i] = images[i];
				while(true) {
					imagesParent[i] = imagesParent[i].parentNode;
					if(imagesParent[i] == document.body) break;
					style = getComputedStyle(imagesParent[i]);
					if(style.overflow == 'auto' || style.overflow == 'scroll') break;
				}
			} else if(imagesHeight[i] != height && rect.top < 0) {
				if(imagesParent[i] == document.body)
					scrollBy(0, height - imagesHeight[i]);
				else
					imagesParent[i].scrollTop += height - imagesHeight[i];
			}
			imagesHeight[i] = height;
		}
		requestAnimationFrame(update);
	};
	update();
})();