Greasy Fork

来自缓存

Greasy Fork is available in English.

返回顶部按钮

在网页右下角添加返回顶部按钮

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         返回顶部按钮
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  在网页右下角添加返回顶部按钮
// @author       You
// @match        *://*/*
// @exclude      *://cuntou0906.github.io/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 创建按钮元素
    const scrollTopBtn = document.createElement('div');

    // 设置按钮样式
    scrollTopBtn.style.cssText = `
        position: fixed;
        right: 20px;
        bottom: 20px;
        width: 32px;
        height: 32px;
        border-radius: 50%;
        background-color: rgba(128, 128, 128, 0.6);
        color: #fff;
        cursor: pointer;
        display: flex;
        justify-content: center;
        align-items: center;
        opacity: 0;
        transition: opacity 0.3s, background-color 0.3s;
        z-index: 9999;
        font-size: 16px;
        user-select: none;
        border: none;
    `;

    // 添加向上箭头
    scrollTopBtn.innerHTML = '↑';

    // 添加鼠标悬停效果
    scrollTopBtn.onmouseover = function() {
        this.style.backgroundColor = 'rgba(128, 128, 128, 0.8)';
    };
    scrollTopBtn.onmouseout = function() {
        this.style.backgroundColor = 'rgba(128, 128, 128, 0.6)';
    };

    // 点击事件处理
    scrollTopBtn.onclick = function() {
        window.scrollTo({
            top: 0,
            behavior: 'smooth'
        });
    };

    // 监听滚动事件,控制按钮显示/隐藏
    window.addEventListener('scroll', function() {
        if (window.scrollY > 200) {
            scrollTopBtn.style.opacity = '1';
        } else {
            scrollTopBtn.style.opacity = '0';
        }
    });

    // 将按钮添加到页面
    document.body.appendChild(scrollTopBtn);
})();