Back to top

A floating pill that takes you back to the top of a long page. Black with white caps and a thin white stroke, so it shows on white, newsprint and black.

Script

The pill starts hidden. A few lines show it once the page’s opening block is out of view, and send focus to the top on click.

<a class="sys-to-top" href="#top"><svg viewBox="0 0 12 12" aria-hidden="true"><path d="M6 11V1.5M1.5 6 6 1.5 10.5 6"/></svg>Top</a>

const top = document.querySelector('.sys-to-top'), intro = document.querySelector('.intro'),   // the opening block
      target = document.getElementById('top');                                               // the h1, with tabindex="-1"
new IntersectionObserver(([e]) => top.classList.toggle('is-shown', !e.isIntersecting)).observe(intro);
top.addEventListener('click', e => {
  e.preventDefault();
  const still = matchMedia('(prefers-reduced-motion: reduce)').matches;
  scrollTo({top: 0, behavior: still ? 'auto' : 'smooth'});
  target.focus({preventScroll: true});
});

Parts

PartMarkupLook
Pilla.sys-to-top pointing at the page’s top40px tall, black, 1.5px white stroke, 13px white caps; 44px hit area
ArrowInline svg, first inside the link12px up arrow, drawn in the type color
Shown.is-shown, added by scriptFades and rises 8px into place

Stipulations