// ==UserScript== // @name 回到顶部 // @version 1.0.1 // @description 支持在所有页面的右下角生成一个顺滑回到顶部的按钮 // @author @leo // @match *://*/* // @grant GM_notification // @grant GM_addStyle // @namespace https://greasyfork.org/users/810117 // @downloadURL none // ==/UserScript== ;(() => { // 样式注入 GM_addStyle(` .GO_TO_TOP_button { width: 42px; height: 42px; border-radius: 8px; box-shadow: 0 3px 6px rgb(0 0 0 / 16%), 0 1px 2px rgb(0 0 0 / 23%); position: fixed; right: 16px; bottom: 16px; display: flex; align-items: center; justify-content: center; z-index: 99999999; } .GO_TO_TOP_button svg { width: 24px; height: 24px; } `) const sleep = delay => { return new Promise(resolve => { setTimeout(() => { resolve() }, delay) }) } // 创建页面回到顶部方法 const goToTop = async () => { let g = 1 if (window.scrollY >= 5000) { g = window.scrollY / 5000 } // 计算距离 while (window.scrollY !== 0) { window.scrollTo(window.scrollX, window.scrollY - 170 * g) await sleep(17) } } // 创建DOM绑定方法 const button = document.createElement('div') button.className = 'GO_TO_TOP_button' button.onclick = goToTop button.innerHTML = '' document.body.appendChild(button) })()