Smart scrolling

Prevent scrolling issues when page is resized by loading images

目前為 2016-06-07 提交的版本,檢視 最新版本

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

(function() {
	var images = [].slice.call(document.getElementsByTagName('IMG'));
	var imagesHeight = [];
	var imagesParent = [];
	(function() {
		var style;
		for(var i = 0; i < images.length; i++) {
			if(!images[i].offsetParent || getComputedStyle(images[i]).position == 'fixed') {
				images.splice(i--, 1);
			} else {
				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;
				}
			}
		}
	})();
	if(images.length == 0) return;
	var update = function() {
		var rect, height;
		for(var i = 0; i < images.length; i++) {
			rect   = images[i].getBoundingClientRect();
			height = Math.round(rect.bottom - rect.top);
			if(!isNaN(imagesHeight[i]) && imagesHeight[i] != height && rect.bottom < 0) {
				if(imagesParent[i] == document.body)
					scrollBy(0, height - imagesHeight[i]);
				else
					imagesParent[i].scrollTop += height - imagesHeight[i];
			}
			imagesHeight[i] = height;
		}
		requestAnimationFrame(update);
	};
	update();
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址