Greasy Fork

Greasy Fork is available in English.

Geekhub scrollTop

Geekhub 返回顶部

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Geekhub scrollTop
// @namespace    http://wenyi.me
// @version      0.4
// @description  Geekhub 返回顶部
// @author       SeaMonster
// @match        https://*.geekhub.com/*
// @grant        none
// ==/UserScript==

try {
    var body = document.body;
    var div = document.createElement("div");
    div.id = "myBtn";
    div.setAttribute("style",
        "display: none;position: fixed;bottom: 160px;right: 50px;z-index: 99; outline: none;background-color: #fff;color: #333;cursor: pointer;border-radius: 10px;box-shadow: 0 2px 4px 0 rgba(0,0,0,.05);"
    );
    div.innerHTML = '<button onclick="topFunction()" style="padding: 15px;" id="myBtn" title="回顶部">Top</button>';
    body.appendChild(div);
} catch (e) {
    console.log("scroll Error");
}
// 当网页向下滑动 300px 出现"返回顶部" 按钮
window.onscroll = function () {
    scrollFunction()
};

function scrollFunction() {
    console.log(121);
    if (document.body.scrollTop > 300 || document.documentElement.scrollTop > 300) {
        document.getElementById("myBtn").style.display = "block";
    } else {
        document.getElementById("myBtn").style.display = "none";
    }
}
// 点击按钮,返回顶部
window.topFunction = function topFunction() {
    (function smoothscroll() {
        var currentScroll = document.documentElement.scrollTop || document.body.scrollTop;
        if (currentScroll > 0) {
            window.requestAnimationFrame(smoothscroll);
            window.scrollTo(0, currentScroll - (currentScroll / 5));
        }
    })();
}