GitHub Scroll To Top

C Pritch
2025-04-11T12:23:23Z

Adds a scroll to top button to github

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:

Install on 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.