โœฆ StealThis .dev

Back to Top

Floating back-to-top button that appears after scrolling down, with smooth scroll animation and fade in/out.

Open in Lab
vanilla-js css
Targets: JS HTML

Code

Back to Top

A floating action button that fades in after the user scrolls 300 px down the page and smooth-scrolls back to the top on click. No dependencies.

Features

  • Fades and slides in from the bottom-right after 300 px of scroll
  • Fades out when the page is near the top
  • Smooth scroll to top via window.scrollTo({ behavior: 'smooth' })
  • Scale animation on hover for tactile feedback
  • Reading-progress ring that fills as the user scrolls down the page
  • Fully keyboard-accessible โ€” focusable and activatable with Enter/Space

How it works

  1. A passive scroll event listener compares window.scrollY against the threshold (300 px)
  2. When the threshold is crossed the button receives an .visible class โ€” a CSS transition handles the fade/slide animation
  3. A <circle> SVG stroke-dashoffset is updated on scroll to draw the circular progress ring
  4. Clicking the button calls window.scrollTo({ top: 0, behavior: 'smooth' })

Accessibility

The button uses aria-label="Back to top" and is always in the tab order when visible. It is hidden via pointer-events: none and opacity: 0 (not display: none) so focus management is smooth.