Greasy Fork is available in English.

Lite Add button for Smooth Scroll to the top / bottom

为页面添加按钮,平滑的滚动到顶部/底部

2015/08/01時点のページです。最新版はこちら。

作者のサイトでサポートを受ける。または、このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください。
// ==UserScript==
// @name	Lite Add button for Smooth Scroll to the top / bottom
// @author	burningall
// @description	为页面添加按钮,平滑的滚动到顶部/底部
// @version     2015.8.1
// @include		*
// @supportURL		http://www.burningall.com
// @contributionURL	troy450409405@gmail.com|alipay.com
// @namespace https://greasyfork.org/zh-CN/users/3400-axetroy
// ==/UserScript==
//=======快捷键======
//alt+1>>>>>>回到顶部
//alt+2>>>>>>去到底部
(function(document){
//================公共函数区============
function addEvent(obj, event, fn) {return obj.addEventListener ? obj.addEventListener(event, fn, false) : obj.attachEventListener("on" + event, fn);}
function getSize(obj) {return document.documentElement[obj]!==0 ? document.documentElement[obj]: document.body[obj];}
function scroll(obj,dir){//obj随意,dir>0往上滚,dir<0往下滚
	clearInterval(obj.timerScroll);
	obj.timerScroll=setInterval(function(){
		var position,speed;
		if(dir>0){//往上滚动
			speed = (getSize('scrollTop') / 10) + 1;
			position = getSize('scrollTop') - speed;
			if (position <= 0) {//如果滚到顶部
				document.body.scrollTop = document.documentElement.scrollTop = 0;
				clearInterval(obj.timerScroll);
				console.timeEnd('hello');
			}
		}else{//往下滚动
			speed = ((getSize('scrollHeight')-getSize('scrollTop')-getSize('clientHeight')) / 10) + 1;
			position = getSize('scrollTop') + speed;
			if (position + getSize('clientHeight') >= getSize('scrollHeight')) {//如果滚到底部
				document.body.scrollTop = document.documentElement.scrollTop = getSize('scrollHeight');
				clearInterval(obj.timerScroll);
			}
		}
		document.body.scrollTop = document.documentElement.scrollTop = position;		
	},20);
}
//================快捷键区============
addEvent(document,'keydown',function(e){
	e=e || window.top.event;
	if(e.altKey && e.keyCode == 49){//alt+1,向上滚动
		scroll(document,1);
	}else if(e.altKey && e.keyCode == 50) { //alt+2,向下滚动
		scroll(document,-1);
	}
});
})(document);