妖火快速回顶部(改)

解放双手!点击直接返回页面顶部,再也不用一点一点往上滑啦!

目前為 2024-11-20 提交的版本,檢視 最新版本

// ==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
// ==/UserScript==

(function() {
    const svg = `
        <svg width="24" height="24" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M24.0083 14.1006V42.0001" stroke="#eee" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/>
            <path d="M12 26L24 14L36 26" stroke="#eee" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/>
            <path d="M12 6H36" stroke="#eee" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/>
        </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 });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址