大网数据广告活动
Web 标准与前端开发

ScrollTimeline

· 52CE 我爱测

The **`ScrollTimeline`** interface of the Web Animations API represents a scroll progress timeline (see CSS scroll-driven animations for more details).

Web Animations

The ScrollTimeline interface of the Web Animations API represents a scroll progress timeline (see CSS scroll-driven animations for more details).

Pass a ScrollTimeline instance to the Animation.Animation constructor or the Element.animate() method to specify it as the timeline that will control the progress of the animation.

Constructor

  • ScrollTimeline.ScrollTimeline
    • : Creates a new ScrollTimeline object instance.

Instance properties

This interface also inherits the properties of its parent, AnimationTimeline.

  • ScrollTimeline.source
    • : Returns a reference to the scrollable element (scroller) whose scroll position is driving the progress of the timeline and therefore the animation.
  • ScrollTimeline.axis
    • : Returns an enumerated value representing the scroll axis that is driving the progress of the timeline.

Instance methods

This interface inherits the methods of its parent, AnimationTimeline.

Examples

Displaying the source and axis of a scroll progress timeline

In this example, we animate an element with a class of box along a view progress timeline — it animates across the screen as the document scrolls. We output the source element and scroll axis to an output element in the top-right corner.

HTML

The HTML for the example is shown below.

<div class="content"></div>
<div class="box"></div>
<div class="output"></div>

CSS

The CSS for the example looks like this:

.content {
  height: 2000px;
}

.box {
  width: 100px;
  height: 100px;
  border-radius: 10px;
  background-color: rebeccapurple;
  position: fixed;
  top: 20px;
  left: 0%;
}

.output {
  font-family: "Helvetica", "Arial", sans-serif;
  position: fixed;
  top: 5px;
  right: 5px;
}

JavaScript

In the JavaScript, we grab references to the box and output <div>s, then create a new ScrollTimeline, specifying that the element that will drive the scroll timeline progress is the document (html) element, and the scroll axis is the block axis. We then animate the box element with the Web Animations API. Last of all, we display the source element and axis in the output element.

const box = document.querySelector(".box");
const output = document.querySelector(".output");

const timeline = new ScrollTimeline({
  source: document.documentElement,
  axis: "block",
});

box.animate(
  {
    rotate: ["0deg", "720deg"],
    left: ["0%", "100%"],
  },
  {
    timeline,
  },
);

output.textContent = `Timeline source element: ${timeline.source.nodeName}. Timeline scroll axis: ${timeline.axis}`;

Result

Scroll to see the box being animated.

Displaying the source and axis of a scroll progress timeline

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。