Smart scrolling

Prevent scrolling issues when page is resized by loading images

当前为 2016-06-06 提交的版本,查看 最新版本

// ==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();
})();

QingJ © 2025

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