返回顶部脚本

网站页面返回顶部脚本

目前為 2019-04-26 提交的版本,檢視 最新版本

// ==UserScript==
// @name         返回顶部脚本
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  网站页面返回顶部脚本
// @author       xianlechuanshuo
// @match       *://**/**
// @require      https://cdn.bootcss.com/jquery/1.12.4/jquery.js
// @grant        none
// ==/UserScript==
var JQ1=jQuery.noConflict();
JQ1(function() {
    'use strict';
    let topTargetHtml=`<a id="top"></a>`;
    JQ1("body").prepend(topTargetHtml);
    let url=window.location.href;//加上当前url,避免跳到其他页面去
    url=url.replace('#top','');//避免出现重复的"#top"
    let toTopHtml=`<div id="to_top">
                    <a href="${url}#top">返回顶部</a>
                   </div>`;
    let toTopEle=JQ1(toTopHtml);
 
    JQ1("body").append(toTopHtml);
    JQ1("#to_top").css(
    {
          "text-align":"center",
          "background":"#06c",
          "display":"none",
          "position":"fixed",
          "right":"1px",
          "bottom":"10px"
    });
    JQ1("#to_top>a").css(
    {
          "text-decoration": "none",
          "display": "block",
          "outline-style":"none",
          "color":"#fff",
          "width":"30px",
          "height":"40px",
          "padding":"20px",
          "font":"14px/20px arial"
    });
});
JQ1(window).scroll(function(){
    //let t=document.documentElement.scrollTop||document.body.scrollTop;
    let t=JQ1(document).scrollTop();
    if(t>100){
        JQ1("#to_top").css(
        {
            "display": "block"
        });
    }
    else{
        JQ1("#to_top").css(
        {
            "display": "none"
        });
    }
})