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

SnapEvent

· 52CE 我爱测

The **`SnapEvent`** interface defines the event object for the Element/scrollsnapchanging_event and Element/scrollsnapchange_event events. Respectively, these fire on a scroll container when the browser determines that a new scroll snap target is pending (will be selected when th

Snap Events

The SnapEvent interface defines the event object for the Element/scrollsnapchanging_event and Element/scrollsnapchange_event events. Respectively, these fire on a scroll container when the browser determines that a new scroll snap target is pending (will be selected when the current scroll gesture ends), and when a new snap target is selected.

These events can be used to run code in response to new elements being snapped to; SnapEvent exposes references to the element snapped to in the inline and/or block direction. The property values available on SnapEvent correspond directly to the value of the scroll-snap-type CSS property set on the scroll container:

  • If the snap axis is specified as block (or a physical axis value that equates to block in the current writing mode), only SnapEvent.snapTargetBlock returns an element reference.
  • If the snap axis is specified as inline (or a physical axis value that equates to inline in the current writing mode), only SnapEvent.snapTargetInline returns an element reference.
  • If the snap axis is specified as both, snapTargetBlock and snapTargetInline return an element reference.

Constructor

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

Instance properties

Inherits properties from its parent, Event.

  • SnapEvent.snapTargetBlock
    • : Returns a reference to the element snapped to in the block direction when the event fired, or null if scroll snapping only occurs in the inline direction so no element is snapped to in the block direction.
  • SnapEvent.snapTargetInline
    • : Returns a reference to the element snapped to in the inline direction when the event fired, or null if scroll snapping only occurs in the block direction so no element is snapped to in the inline direction.

Examples

scrollsnapchanging example

In the following scrollsnapchanging handler function snippet, we set the SnapEvent.snapTargetBlock element's class attribute to pending using the Element.className property, which could be used to style the element differently when it becomes a pending snap target.

Note that this handler is intended to be set on a block-direction scroll container (vertically-scrolling if the page is set to a horizontal writing-mode), therefore only the snapTargetBlock element will change between multiple events firing. SnapEvent.snapTargetInline will return null, because no snapping occurs in the inline direction.

scrollingElem.addEventListener("scrollsnapchanging", (event) => {
  // Set current pending snap target class to "pending"
  event.snapTargetBlock.className = "pending";

  // Logs the new pending block-direction snap target element
  console.log(event.snapTargetBlock);

  // Logs null; no inline snapping occurs
  console.log(event.snapTargetInline);
});

scrollsnapchange example

In the following scrollsnapchange handler function snippet, we set a selected class on the SnapEvent.snapTargetBlock element, which could be used to style a newly-selected snap target to look like it has been selected (for example, with an animation).

scrollingElem.addEventListener("scrollsnapchange", (event) => {
  event.snapTargetBlock.className = "selected";
});

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

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

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