GitHub Scroll To Top

Adds a scroll to top button to github

作者
C Pritch
今日安裝
0
安裝總數
1
評價
0 0 0
版本
2025-04-11
建立日期
2025-04-11
更新日期
2025-04-11
尺寸
3.8 KB
授權條款
MIT
腳本執行於

GitHub Scroll To Top Butto

Summary

This userscript adds a convenient "Scroll to Top" button to GitHub pages. The button remains hidden until you scroll down the page, at which point it smoothly slides into view in the bottom-right corner. Clicking the button provides a smooth scroll animation back to the top of the page.

Features

  • Smooth Scrolling: Uses window.scrollTo({ behavior: 'smooth' }) for a pleasant scroll-to-top experience.
  • Animated Appearance: The button fades and slides into view only when you've scrolled down a certain amount (default: 200 pixels), keeping the UI clean when near the top.
  • Fixed Positioning: Stays accessible in the bottom-right corner regardless of scroll position (once visible).
  • Dynamic Loading: Waits for GitHub's main content area (.application-main) to appear before adding the button, ensuring compatibility with GitHub's dynamic page loading.
  • Simple Styling: Basic, clean button style that fits reasonably well with GitHub's UI.

How It Works

  1. The script runs early (@run-at document-start) to set up an observer.
  2. It waits for the document.body to be available.
  3. It checks if the element with the class .application-main exists.
    • If yes, it adds the button immediately.
    • If not, it uses a MutationObserver to watch for when .application-main is added to the page.
  4. Once the button is added:
    • It's initially styled to be hidden (opacity: 0) and positioned slightly off-screen (transform: translateY(50px)).
    • A CSS transition is applied for smooth animation effects.
    • A scroll event listener is attached to the window.
  5. When the user scrolls:
    • The listener checks if the vertical scroll distance (window.scrollY) exceeds a threshold (default: 200px).
    • If it exceeds the threshold, the button's style is updated to opacity: 1 and transform: translateY(0), causing it to slide/fade in.
    • If it's below the threshold, the styles are reverted, hiding the button again.
  6. Clicking the button triggers window.scrollTo({ top: 0, behavior: 'smooth' }).

Installation

  1. Ensure you have a userscript manager extension installed in your browser (e.g., Tampermonkey, Greasemonkey, Violentmonkey).
  2. Install the script directly from Greasy Fork镜像:
  3. Your userscript manager should prompt you to install the script. Confirm the installation. Updates will also be handled through Greasy Fork镜像 and your manager.

Customization

You can adjust the scroll distance required before the button appears by changing the scrollThreshold variable near the top of the script code (default is 200). If you modify the script locally, be aware that updates from Greasy Fork镜像 might overwrite your changes unless you disable automatic updates for this script in your manager.

QingJ © 2025

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