// ==UserScript== // @name 妖火快速回顶部(改) // @namespace http://tampermonkey.net/ // @version 1.0.1 // @description 解放双手!点击直接返回页面顶部,再也不用一点一点往上滑啦! // @license MIT // @author cradms // @match *://yaohuo.me/bbs* // @match *://www.yaohuo.me/bbs* // @icon https://www.google.com/s2/favicons?sz=64&domain=yaohuo.me // @grant none // @downloadURL none // ==/UserScript== (function() { const svg = ` `; const dom = document.createElement('div'); dom.innerHTML = svg; Object.assign(dom.style, { position: 'fixed', right: '20px', bottom: '20px', width: '40px', height: '40px', backgroundColor: '#4595d5', borderRadius: '4px', fontSize: '15px', textAlign: 'center', lineHeight: '40px', cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', visibility: 'hidden' }); document.body.appendChild(dom); function scrollToTop({ el, speed = 5, top = 0, distance = 300 }) { window.addEventListener('scroll', () => { el.style.visibility = (document.documentElement.scrollTop || document.body.scrollTop) > distance ? 'visible' : 'hidden'; }); el.addEventListener('click', () => { const scrollStep = () => { const currentScroll = document.documentElement.scrollTop || document.body.scrollTop; if (currentScroll > top) { window.requestAnimationFrame(scrollStep); window.scrollTo(0, currentScroll - (currentScroll / speed)); } }; scrollStep(); }); } scrollToTop({ el: dom }); })();