-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgotop.js
31 lines (30 loc) · 980 Bytes
/
gotop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
ページのトップへスクロールして移動
(c)Sapphirus.Biz
HREFに「#TOP」(大文字・小文字の区別は無し)があるリンクを
クリックした場合、スクロールしてページの一番上に移動します。
Ex.) <a href="#TOP">PAGE TOP</a>
*/
function setGoTop() {
var aTagList = document.getElementsByTagName('a');
for (var i = 0; i < aTagList.length; i++) {
if (aTagList[i].href.match(/#top/i)) {
aTagList[i].onclick = goPageTop;
}
}
}
var goTopMove = 20; // 加速度(0:停止~大きいほど遅くなる)
var goTopPosi;
function goPageTop() { // 距離取得と実行
var yPos = document.body.scrollTop || document.documentElement.scrollTop;
mObj(yPos);
return false;
}
function mObj(y, s) { // 上に加速移動
if (s) goTopMove = s;
goTopPosi = parseInt(y - y * 2 / goTopMove);
scrollTo(0, goTopPosi);
if (goTopPosi > 0) setTimeout('mObj(goTopPosi, goTopMove)', 1);
}
if (window.addEventListener) window.addEventListener('load', setGoTop, false);
if (window.attachEvent) window.attachEvent('onload', setGoTop);