π’ Scroll-Driven Animations: Making Pages Dance As You Scroll!
π¬ The Story Beginsβ¦
Imagine you have a magic scroll β like a wizardβs spell book. As you roll it open, pictures come alive! Birds start flying, flowers bloom, and dragons breathe fire.
Thatβs exactly what scroll-driven animations do on websites! When someone scrolls down a page, things move, fade, grow, or dance β all controlled by how much theyβve scrolled.
π What Are Scroll-Driven Animations?
Think of a flipbook. You know those little books where you flip pages quickly and it looks like a cartoon is moving?
Now imagine that instead of flipping pages, youβre scrolling on your phone or computer. The more you scroll, the more the animation plays!
Before scroll-driven animations:
- Animations just played automatically when the page loaded
- You had no control β like watching a movie that wonβt pause
Now with scroll-driven animations:
- YOU control the animation by scrolling!
- Scroll up = animation goes backward
- Scroll down = animation goes forward
- Itβs like being the director of your own movie! π₯
π― The Two Magic Spells: scroll() and view()
CSS gives us two special functions to create these scroll-controlled animations:
| Function | What It Does |
|---|---|
scroll() |
Watches the whole page or a scrolling box |
view() |
Watches one specific element as it enters and leaves the screen |
Letβs explore each one!
π The scroll() Function β Watching the Whole Journey
What Is It?
The scroll() function is like having a helicopter view of someone climbing a mountain. You can see their whole journey from start to finish.
animation-timeline: scroll();
This tells CSS: βHey! Link my animation to scrolling!β
How Does It Work?
Imagine a progress bar that fills up as you scroll down a page:
.progress-bar {
position: fixed;
top: 0;
left: 0;
height: 5px;
background: blue;
animation: fill-bar linear;
animation-timeline: scroll();
}
@keyframes fill-bar {
from { width: 0%; }
to { width: 100%; }
}
What happens:
- At the top of the page β bar is empty (0%)
- Halfway scrolled β bar is half full (50%)
- At the bottom β bar is completely full (100%)
The scroll() Function Takes Options
You can tell scroll() what to watch and which direction:
animation-timeline: scroll(root block);
First part β What to scroll:
| Value | Meaning |
|---|---|
root |
The whole webpage |
nearest |
The closest scrollable parent |
self |
The element itself if it scrolls |
Second part β Which direction:
| Value | Meaning |
|---|---|
block |
Up/down scrolling (most common!) |
inline |
Left/right scrolling |
π¨ Simple Example: A Rocket That Flies As You Scroll
.rocket {
animation: fly-up linear;
animation-timeline: scroll();
}
@keyframes fly-up {
from {
transform: translateY(100px);
opacity: 0.2;
}
to {
transform: translateY(-200px);
opacity: 1;
}
}
As you scroll down, the rocket moves up and becomes brighter! π
ποΈ The view() Function β Spotlight on One Actor
What Is It?
The view() function is like a spotlight on one specific actor. When that actor walks onto the stage (enters your screen), the animation plays!
animation-timeline: view();
This says: βStart animating when THIS element enters the view!β
How Is It Different from scroll()?
Think of it this way:
| scroll() | view() |
|---|---|
| Watches your scroll position on the whole page | Watches one element entering/leaving the screen |
| Like tracking a carβs journey from city to city | Like watching a car pass through one intersection |
| Animation tied to page scroll progress | Animation tied to element visibility progress |
π The View Timeline Stages
When an element scrolls into view, it goes through stages:
βββββββββββββββββββββββββ
β YOUR SCREEN β
β ββββββββββββββββββββββββ£
βββββββββββββββββββββββββββββββββ
β β Element ENTERING β β
βββββββββββββββββββββββββββββββββ
β β
βββββββββββββββββββββββββββββββββ
β β Element COVERING β β
βββββββββββββββββββββββββββββββββ
β β
βββββββββββββββββββββββββββββββββ
β β Element LEAVING β β
βββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββ
- Entry: Element starts peeking in from the edge
- Cover/Contain: Element is fully visible
- Exit: Element starts leaving the screen
πͺ Simple Example: Cards That Fade In
.card {
animation: fade-in linear both;
animation-timeline: view();
animation-range: entry 0% entry 100%;
}
@keyframes fade-in {
from {
opacity: 0;
transform: translateY(50px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
What happens:
- Card starts invisible and below its position
- As it enters the screen, it fades in and slides up
- Animation completes when card fully enters
ποΈ animation-range: Fine-Tuning Your Animation
Both scroll() and view() use animation-range to control when the animation plays:
With view() β Entry and Exit Ranges
/* Animate during entry only */
animation-range: entry;
/* Animate during exit only */
animation-range: exit;
/* Animate from start of entry to end */
animation-range: entry 0% entry 100%;
/* Animate from entry start to exit end */
animation-range: entry 0% exit 100%;
The Range Keywords
| Keyword | What It Means |
|---|---|
entry |
When element enters the viewport |
exit |
When element leaves the viewport |
contain |
When element is fully inside viewport |
cover |
Full journey: entry β contain β exit |
π¨ Putting It All Together
Example 1: Reading Progress Bar (scroll())
.reading-bar {
position: fixed;
top: 0;
left: 0;
height: 4px;
background: linear-gradient(
to right,
#ff6b6b,
#4ecdc4
);
transform-origin: left;
animation: grow-bar linear;
animation-timeline: scroll();
}
@keyframes grow-bar {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
Example 2: Images Reveal on Scroll (view())
.scroll-image {
animation: reveal linear both;
animation-timeline: view();
animation-range: entry 10% cover 40%;
}
@keyframes reveal {
from {
clip-path: inset(100% 0 0 0);
opacity: 0;
}
to {
clip-path: inset(0 0 0 0);
opacity: 1;
}
}
π§ Quick Comparison
graph TD A["Scroll-Driven Animations"] --> B["scroll function"] A --> C["view function"] B --> D["Tracks page scroll position"] B --> E["Good for: progress bars, parallax"] C --> F["Tracks element visibility"] C --> G["Good for: fade-ins, reveals"]
π Why This Is Amazing!
- No JavaScript needed! Pure CSS magic β¨
- Super smooth β browser handles performance
- User-controlled β scrolling feels interactive
- Reversible β scroll up and animation reverses!
π What You Learned Today
β scroll() watches how far youβve scrolled on the page β view() watches when an element enters/leaves the screen β animation-timeline connects your animation to scrolling β animation-range controls exactly when animation plays β Both make websites feel alive and interactive!
π Youβre Ready!
Now you know how to make webpages dance to the rhythm of scrolling! Every time someone scrolls, your animations respond. Itβs like giving your website a heartbeat that syncs with the user.
Go make something magical! π
