MDN Web Docs

You can define the scroller that controls the animation either by naming the animation or with the scroll() function.

<main>
  <div></div>
</main>

css

main {
  scroll-timeline: --main-timeline;
}
div {
  animation: background-animation linear;
  animation-timeline: scroll(nearest inline);
}
div::after {
  animation: shape-animation linear;
  animation-timeline: --main-timeline;
}
@layer animations {
  @keyframes background-animation {
    0% {
      background-color: palegoldenrod;
    }
    100% {
      background-color: magenta;
    }
  }
  @keyframes shape-animation {
    0% {
      left: 0;
      top: 0;
      background-color: black;
    }
    50% {
      top: calc(100% - var(--elSize));
      left: calc(50% - var(--elSize));
      background-color: red;
    }
    100% {
      left: calc(100vw - var(--elSize));
      top: 0;
      rotate: 1800deg;
      background-color: white;
    }
  }
}
@layer page-setup {
  :root {
    --elSize: 50px;
  }
  main {
    height: 90vh;
    overflow: scroll;
    border: 1px solid;
    margin: 5vh auto;
  }
  div {
    height: 400vh;
    width: 400vw;
  }
  div::after {
    content: "";
    border: 1px solid red;
    height: var(--elSize);
    width: var(--elSize);
    position: absolute;
    border-radius: 20px;
    corner-shape: superellipse(-4);
  }
}
@layer no-support {
  @supports not (scroll-timeline: --main-timeline) {
    body::before {
      content: "Your browser doesn't support scroll-driven animations.";
      background-color: wheat;
      display: block;
      text-align: center;
      padding: 1rem 0;
    }
  }
}

Scroll the element in the inline direction to see its background color change. Scroll it vertically to see the generated content move, spin, and change colors.

Reference

Properties

Data types and values

Functions

Interfaces

Guides

Scroll-driven animation timelines

Scroll-driven animation timelines and creating scroll-driven animations.

Timeline range names

The <timeline-range-name> data type: Understanding the various timeline range names.

Insetting view progress timelines

Insetting the animation attachment ranges of scroll-driven animations.

Specifications

Specification
Scroll-driven Animations

See also

Help improve MDN

Yes No

Learn how to contribute

This page was last modified on by MDN contributors.

Read the original on developer.mozilla.org ↗