Scroll-driven scenes with Lenis and GSAP
Much of this site moves with the scroll: the fog clears as you leave the intro, the camera pulls back from a laptop as you enter the studio. The pattern underneath is small.
A tall section with a sticky stage
The section is several screens tall. Inside it, one element is position: sticky; top: 0; height: 100vh. As you scroll through the section the stage stays put, and you get a progress value from 0 to 1 by comparing the section's top to the viewport.
That progress number is the only thing the scene reads. It does not know about pixels.
Smooth scrolling
Lenis smooths wheel input so progress changes gradually instead of in steps. If you also use GSAP's ScrollTrigger, drive Lenis from GSAP's ticker so both share one clock; two separate animation loops produce a faint jitter that is maddening to track down.
Rules that saved me
- Read layout once per frame, in one place. Several components each calling
getBoundingClientRectadd up. - Stop when off screen. An IntersectionObserver that pauses the render loop is a few lines and saves a great deal of battery.
- Never trap the scroll without a way out. My studio locks scrolling while you explore it, so it always offers buttons to leave in both directions, and Escape works.
- Deep links still have to work. A page that starts at the top on purpose must still honour
/#contact.
When not to
On phones, long scroll-driven scenes fight the browser's own gestures and address bar. There I replaced the scroll-in with a button. Scroll is a good timeline on a desk and an unreliable one in a hand.