Greasy Fork

Greasy Fork is available in English.

滚动条及顶部底部按钮

在任何网页顶部显示滚动进度条,并提供滚动到顶部和底部的按钮,优化性能,减少卡顿现象。

当前为 2024-10-06 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         滚动条及顶部底部按钮
// @namespace    http://greasyfork.icu/
// @version      0.9
// @description  在任何网页顶部显示滚动进度条,并提供滚动到顶部和底部的按钮,优化性能,减少卡顿现象。
// @author       Leo
// @match        *://*/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 获取页面滚动条的位置
    const getScrollTop = () => window.pageYOffset || document.documentElement.scrollTop;

    // 获取页面总高度
    const getPageHeight = () => Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);

    // 获取视口高度
    const getClientHeight = () => window.innerHeight || document.documentElement.clientHeight;

    // 创建进度条元素
    const progressBar = document.createElement('div');
    progressBar.id = 'progress';
    progressBar.style.pointerEvents = 'none'; // 允许点击穿透
    document.body.insertBefore(progressBar, document.body.firstChild);

    // 创建进度条的 bar 元素
    const bar = document.createElement('div');
    bar.className = 'bar';
    Object.assign(bar.style, {
        position: 'fixed',
        zIndex: '999999999',
        top: '0',
        left: '0',
        width: '0%', // 设置初始宽度为 0%
        height: '2px',
        background: 'linear-gradient(90deg, #03a9f4, #f441a5)',
        boxShadow: '0 0 40px #03a9f4, 0 0 50px #f441a5, 0 0 60px #03a9f4', // 增加阴影效果
        borderRadius: '0.5em',
        transition: 'width 0.2s ease' // 优化过渡动画,减轻卡顿
    });

    progressBar.appendChild(bar);

    // 节流函数,用于控制函数调用频率
    const throttle = (fn, delay) => {
        let lastCall = 0;
        return (...args) => {
            const now = Date.now();
            if (now - lastCall >= delay) {
                lastCall = now;
                fn(...args);
            }
        };
    };

    // 更新进度条宽度
    const updateProgress = () => {
        const scrollTop = getScrollTop();
        const pageHeight = getPageHeight();
        const clientHeight = getClientHeight();
        const maxScroll = pageHeight - clientHeight;

        // 避免除以零的情况
        const progress = maxScroll > 0 ? (scrollTop / maxScroll) * 100 : 0;

        // 确保进度不会超过 100%
        bar.style.width = `${Math.min(progress, 100)}%`;
    };

    // 创建滚动按钮
    const createScrollButton = (text, onClick) => {
        const button = document.createElement("button");
        button.innerText = text;
        Object.assign(button.style, {
            padding: '10px',
            marginLeft: '10px', // 设置按钮间的水平间距
            cursor: 'pointer', // 添加鼠标指针样式
            backgroundColor: '#0005',
            color: '#eee',
            border: 'none',
            borderRadius: '5px',
            fontSize: '16px'
        });
        button.onclick = onClick;
        return button;
    };

    // 创建滚动到顶部和底部的按钮
    const createScrollButtons = () => {
        const scrollBtns = document.createElement("div");
        scrollBtns.className = "goto_top_end";
        Object.assign(scrollBtns.style, {
            position: "fixed",
            bottom: "10px",
            right: "5rem", 
            zIndex: "1000000000",
            display: "flex",
            flexDirection: "row", // 设置为 "row" 以实现水平布局
            alignItems: "center"
        });

        const toTop = createScrollButton("⬆️顶部", () => {
            window.scrollTo({ top: 0, behavior: "smooth" });
        });

        const toEnd = createScrollButton("⬇️底部", () => {
            const viewportHeight = getClientHeight();
            const documentHeight = getPageHeight();
            window.scrollTo({ top: documentHeight - viewportHeight, behavior: "smooth" });
        });

        scrollBtns.appendChild(toTop);
        scrollBtns.appendChild(toEnd);
        document.body.appendChild(scrollBtns);
    };

    // 页面加载时初始化
    window.addEventListener('load', () => {
        updateProgress();
        createScrollButtons();
    });

    // 监听滚动事件,使用节流来降低频率
    window.addEventListener('scroll', throttle(updateProgress, 50)); // 调整节流间隔,控制更新频率

})();