Skip to content

Scroll Interactions ​

This page gathers three scroll-related features: smooth scrolling, changing the header style, and a back-to-top button.

Smooth Scrolling ​

This project handles smooth scrolling with a single line of CSS rather than JS's scrollIntoView().

css
html {
  scroll-behavior: smooth;
}

As long as the menu links are anchors like href="#about", the browser scrolls smoothly on its own. The result is the same as a JS implementation, but if the checklist explicitly says "implemented with JS," it's a good idea to point out that the approach is different.

Changing the Header Style & Back-to-Top Button ​

A single scroll event handler (updateScrollUi) handles two things at once.

js
selectors.header.classList.toggle('is-scrolled', scrollY >= 60);
selectors.topButton.classList.toggle('is-visible', scrollY >= 300);
  • Once you scroll 60px or more, the header gets a background/shadow.
  • At 300px or more, the back-to-top button appears.
  • Clicking the button moves to the very top with window.scrollTo({ top: 0, behavior: 'smooth' }).

Scroll Events and Rendering Performance ​

A scroll event fires dozens of times from just one swipe of a finger. So if the work inside updateScrollUi is heavy, the screen easily becomes choppy.

When the browser changes the screen, it generally goes through the steps below again. The earlier the step it has to restart from, the higher the cost.

Style calculation → Layout (reflow) → Paint (repaint) → Composite
  if size/position changes,  if only color/shadow changes,  if only transform/opacity changes,
  redo from here             redo from here                 redo only this

From this point of view, the code in this project is relatively light.

  • It reads scrollY once and only adds or removes classes. classList.toggle changes nothing if the value is already the same, so the screen is redrawn only at the moment you cross the 60px and 300px boundaries.
  • The header's is-scrolled only changes the background color and shadow, so it redoes only paint, not layout.
  • The listener is registered with addEventListener('scroll', updateScrollUi, { passive: true }). passive: true is an option that tells the browser in advance "this listener will not block scrolling with preventDefault()," so the browser can scroll right away without waiting for the listener to finish.

However, when adding more features, it's good to be careful about the following.

Watch out forWhyAlternative
Repeatedly reading offsetTop, getBoundingClientRect() and writing on every scrollEvery read forces the browser to recalculate layout immediatelyBatch the reads first, then do the writes afterward
Directly changing top, height based on scroll positionCauses a reflow every timeMove things with transform
Calculating "is the element visible" in the scroll eventThe calculation happens often and takes longIntersectionObserver (used in Animation Effects)
Heavy calculations inside the handlerThe screen stutters while scrollingProcess only once per frame with requestAnimationFrame

What reflow and repaint are is covered in detail in the How Browsers Work article.

Summary ​

FeatureApproach
Smooth scrollingCSS scroll-behavior: smooth
Changing the header styleScroll 60px or more → is-scrolled
Back-to-top buttonScroll 300px or more → is-visible, scrollTo on click
PerformanceLight, since it only toggles classes. When adding features, avoid repeated layout reads/writes

Source: Codyssey-B1/B1-1