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().
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.
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 thisFrom this point of view, the code in this project is relatively light.
- It reads
scrollYonce and only adds or removes classes.classList.togglechanges 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-scrolledonly changes the background color and shadow, so it redoes only paint, not layout. - The listener is registered with
addEventListener('scroll', updateScrollUi, { passive: true }).passive: trueis an option that tells the browser in advance "this listener will not block scrolling withpreventDefault()," 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 for | Why | Alternative |
|---|---|---|
Repeatedly reading offsetTop, getBoundingClientRect() and writing on every scroll | Every read forces the browser to recalculate layout immediately | Batch the reads first, then do the writes afterward |
Directly changing top, height based on scroll position | Causes a reflow every time | Move things with transform |
| Calculating "is the element visible" in the scroll event | The calculation happens often and takes long | IntersectionObserver (used in Animation Effects) |
| Heavy calculations inside the handler | The screen stutters while scrolling | Process only once per frame with requestAnimationFrame |
What reflow and repaint are is covered in detail in the How Browsers Work article.
Summary
| Feature | Approach |
|---|---|
| Smooth scrolling | CSS scroll-behavior: smooth |
| Changing the header style | Scroll 60px or more → is-scrolled |
| Back-to-top button | Scroll 300px or more → is-visible, scrollTo on click |
| Performance | Light, since it only toggles classes. When adding features, avoid repeated layout reads/writes |
Source: Codyssey-B1/B1-1
