# Scroll-Driven & Reduced Motion

> Animate elements as you scroll, and learn how to respect user motion preferences.

Subject: CSS | Lesson 33 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/scroll-driven-and-reduced-motion
Previous: [Transforms (2D & 3D)](https://codewithanimation.in/css/transforms.md) | Next: [Filters & Blend Modes](https://codewithanimation.in/css/filters-and-blend-modes.md)

## What are Scroll-driven Animations?

Historically, tying an animation to a user's scroll position required heavy JavaScript event listeners that often caused performance lag (jank).

Modern CSS introduced `animation-timeline`, allowing you to link an `@keyframes` animation directly to the scroll progress of the page or a container. As you scroll down, the animation scrubs forward; as you scroll up, it scrubs backward.

## Why does Reduced Motion matter?

While scroll-linked parallax and zooming effects look incredibly cool, they can induce nausea, dizziness, or headaches in users with vestibular disorders.

Operating systems have a 'Reduce Motion' accessibility setting. As a developer, it is your absolute responsibility to read this setting using the `@media (prefers-reduced-motion: reduce)` query and disable heavy movement.

## How do you use them?

Instead of an `animation-duration`, you assign a timeline. `animation-timeline: scroll()` links the animation to the nearest scrolling parent.

Alternatively, `animation-timeline: view()` tracks when the specific element enters and exits the viewport.

To handle accessibility, wrap your animations in `@media (prefers-reduced-motion: no-preference)` so they only play for users who haven't explicitly opted out of motion.

## Common mistake: The infinite scroll trap

If you use `animation-timeline: scroll()` on an element, the animation spans the entire scrollable height of the page. This means the element might finish its animation long before or after it is actually visible to the user. Use `view()` for entry/exit animations.

## Tip: Graceful degradation

Scroll-driven animations are still a relatively new CSS feature. If a browser doesn't support them, the `animation-timeline` property is ignored. Ensure your elements look good in their default 100% state so the page remains perfectly usable without the animation!

## Try it: Try it

Scroll the panel below to see a native CSS scroll-driven animation in action, and observe how the media query safely neutralizes it.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/scroll-driven-and-reduced-motion

## Check yourself

### Question 1

What is the primary benefit of native CSS scroll-driven animations over JavaScript scroll listeners?

- A. They run on the compositor thread and don't block the main thread, resulting in buttery smooth 60fps.
- B. They automatically generate polyfills for older browsers like Internet Explorer 11.
- C. They automatically disable themselves when reduced-motion is requested.

Correct answer: A

JavaScript scroll events fire on the main thread and can easily cause visual stuttering (jank). Native CSS animations are handed off to the GPU and composited smoothly.

- Why not B: They do not generate polyfills; they rely on graceful degradation.
- Why not C: You still have to manually write the @media query to respect reduced motion.

### Question 2

Which value tracks an element's journey specifically as it crosses the viewport?

- A. animation-timeline: scroll()
- B. animation-timeline: view()
- C. animation-timeline: element()

Correct answer: B

The `view()` timeline is perfect for reveal animations, as 0% represents the moment the element enters the screen, and 100% represents the moment it leaves.

- Why not A: scroll() tracks the entire scrollable area of a scroll container, from top to bottom.
- Why not C: This is not a valid CSS value for animation-timeline.

### Question 3

How should you handle @media (prefers-reduced-motion: reduce)?

- A. Hide the element completely using display: none.
- B. Strip away sweeping layout movements, replacing them with instant state changes or subtle fades.
- C. Slow the animation down by doubling its duration.

Correct answer: B

Reduced motion doesn't mean 'no animation at all'—it means removing physical travel, zooming, and spinning. A simple crossfade is usually perfectly fine.

- Why not A: Users with vestibular disorders still need to see your content!
- Why not C: Slow-moving sweeping animations can actually be more nauseating than fast ones.

## Remember this

- Use animation-timeline: scroll() for progress bars or global scroll tracking.
- Use animation-timeline: view() for on-scroll reveal effects.
- Always check @media (prefers-reduced-motion: reduce).
- Replace sweeping movement with subtle opacity fades for users who request reduced motion.
