🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Smart scrolling

Prevent scrolling issues when page is resized by loading images

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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.

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

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

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.4
// @grant       none
// @run-at      document-start
// ==/UserScript==

(function() {
	var hash = location.hash.replace("#", "");
	if(hash) {
		var scrollListener = function() {removeEventListener("wheel", scrollListener); hash = "";};
		addEventListener("wheel", scrollListener);
	}
	var images;
	var update = function() {
		var scrollY = 0, ready = document.readyState == "complete", elem;
		if(hash) {
			elem = document.getElementById(hash) || document.getElementsByName(hash)[0];
			if(elem) scrollY = Math.round(elem.getBoundingClientRect().top);
		} else if(document.readyState == "interactive") {
			if(!images) images = [].slice.call(document.getElementsByTagName("IMG"));
			var 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();
})();