GitHub Scroll To Top

Adds a scroll to top button to github

目前為 2025-04-11 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

作者
C Pritch
評價
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.