Animation Effects (Scroll Reveal & Typing)
This page gathers two effects that add motion to the screen.
Scroll Reveal Animation
setupRevealAnimation() creates an IntersectionObserver with threshold: 0.2 and observes the elements that have the .reveal class.
When an element is 20% or more inside the viewport, the is-visible class is added, and observation stops immediately with observer.unobserve() so the animation doesn't run repeatedly on the same element.
Why animate with transform and opacity?
This project's utilities.css hides .reveal with opacity: 0 and transform: translateY(28px), and when is-visible is added, the element rises into place and appears over 600ms. You could create the same effect with the top value, but the amount of work the browser does varies greatly depending on which property you change.
/* Heavy approach: the position changes, so layout is recalculated from scratch every frame */
.reveal { position: relative; top: 28px; }
.reveal.is-visible { top: 0; transition: top 0.6s; }
/* Light approach (this project's actual code): move the already-drawn picture and change only opacity */
.reveal {
opacity: 0;
transform: translateY(28px);
transition: opacity 600ms ease, transform 600ms ease;
}
.reveal.is-visible {
opacity: 1;
transform: translateY(0);
}| Property changed | Steps redone | Cost |
|---|---|---|
top, left, width, height, margin | Layout → Paint → Composite | High (even positions of surrounding elements are recalculated) |
color, background, box-shadow | Paint → Composite | Medium |
transform, opacity | Composite | Low (often handled by the GPU) |
Animations redraw the screen around 60 times per second, so whenever possible, changing only transform and opacity keeps them smooth. This project is already written the light way. The layout, paint and composite steps are covered in detail in the How Browsers Work article.
Something not there yet: for users who feel dizzy from motion, turning off animations when "Reduce motion" is enabled in the operating system is also good UX consideration.
@media (prefers-reduced-motion: reduce) {
.reveal { opacity: 1; transform: none; transition: none; }
}Hero Typing Effect
typeHeroText() cycles through an array of phrases (phrases), adding and deleting one character at a time. The four delays below are handled with separate setTimeouts to imitate the rhythm of a person typing.
| Stage | Delay |
|---|---|
| Typing one character | 110ms |
| Deleting one character | 55ms |
| Pause after a phrase is complete | 1300ms |
| Switch to the next phrase after deletion finishes | 250ms |
Summary
- Scroll reveal: runs only once, when the element becomes visible, using
IntersectionObserver - Creating motion with
transformandopacityavoids redoing layout, so it's smooth - Respect the reduce-motion setting with
prefers-reduced-motion - Typing: giving each stage a different
setTimeoutdelay creates a natural rhythm
Source: Codyssey-B1/B1-1
