// ==UserScript== // @name 网页回到顶部按钮 // @namespace https://scriptcat.org/ // @version 1.0.0 // @description 在任意网页右下角添加一个"回到顶部"按钮,滚动超过300px时显示 // @author WorkBuddy // @match *://*/* // @grant none // ==/UserScript== (function () { 'use strict'; // 创建按钮 const btn = document.createElement('button'); btn.innerHTML = '↑'; btn.title = '回到顶部'; btn.setAttribute('aria-label', '回到顶部'); btn.style.cssText = ` position: fixed; right: 24px; bottom: 24px; width: 48px; height: 48px; border-radius: 50%; background: #4CAF50; color: #fff; border: none; font-size: 24px; line-height: 48px; text-align: center; cursor: pointer; box-shadow: 0 2px 10px rgba(0,0,0,0.3); z-index: 99999; display: none; transition: opacity 0.3s; `; document.body.appendChild(btn); // 监听滚动 window.addEventListener('scroll', () => { btn.style.display = window.scrollY > 300 ? 'block' : 'none'; }, { passive: true }); // 点击回到顶部 btn.addEventListener('click', () => { window.scrollTo({ top: 0, behavior: 'smooth' }); }); })();