返回顶部按钮

给页面添加返回顶部按钮

目前为 2017-12-20 提交的版本。查看 最新版本

// ==UserScript==
// @name         返回顶部按钮
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  给页面添加返回顶部按钮
// @author       李登科
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var a = document.createElement('a');
    a.id="my-top";
    a.href="#";
    a.text="TOP";
    a.style.position="fixed";
    a.style.right="5%";
    a.style.bottom="25%";
    a.style.fontSize="20px";
    a.style.textDecoration="none";
    a.style.display="none";
    document.querySelector("body").appendChild(a);

    window.addEventListener("scroll", function(){
        if (+(document.body.scrollTop || document.documentElement.scrollTop) > 100) {
            a.style.display = "block";
        } else {
            a.style.display = "none";
        }
    });

})();