IntersectionObserver
接口在
交集观测器 API
provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's
viewport
.
The ancestor element or viewport is referred to as the root.
当
IntersectionObserver
is created, it's configured to watch for given ratios of visibility within the root. The configuration cannot be changed once the
IntersectionObserver
is created, so a given observer object is only useful for watching for specific changes in degree of visibility; however, you can watch multiple target elements with the same observer.
IntersectionObserver.IntersectionObserver()
IntersectionObserver
object which will execute a specified callback function when it detects that a target element's visibility has crossed one or more thresholds.
IntersectionObserver.root
只读
元素
or
Document
whose bounds are used as the bounding box when testing for intersection. If no
root
value was passed to the constructor or its value is
null
, the top-level document's viewport is used.
IntersectionObserver.rootMargin
只读
px
) or as a percentage (
%
). The default is "0px 0px 0px 0px".
IntersectionObserver.thresholds
只读
A list of thresholds, sorted in increasing numeric order, where each threshold is a ratio of intersection area to bounding box area of an observed target. Notifications for a target are generated when any of the thresholds are crossed for that target. If no value was passed to the constructor, 0 is used.
IntersectionObserver.disconnect()
IntersectionObserver
object from observing any target.
IntersectionObserver.observe()
IntersectionObserver
a target element to observe.
IntersectionObserver.takeRecords()
IntersectionObserverEntry
objects for all observed targets.
IntersectionObserver.unobserve()
IntersectionObserver
to stop observing a particular target element.
var intersectionObserver = new IntersectionObserver(function(entries) {
// If intersectionRatio is 0, the target is out of view
// and we do not need to do anything.
if (entries[0].intersectionRatio <= 0) return;
loadItems(10);
console.log('Loaded new items');
});
// start observing
intersectionObserver.observe(document.querySelector('.scrollerFooter'));
| 规范 | 状态 | 注释 |
|---|---|---|
|
Intersection Observer
The definition of 'IntersectionObserver' in that specification. |
工作草案 |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
IntersectionObserver
|
Chrome 51 | Edge 15 |
Firefox
55
|
IE No | Opera 38 | Safari 12.1 | WebView Android 51 | Chrome Android 51 | Firefox Android ? | Opera Android 41 | Safari iOS 12.2 | Samsung Internet Android 5.0 |
IntersectionObserver()
构造函数
|
Chrome 51 | Edge 15 |
Firefox
55
|
IE No | Opera 38 | Safari 12.1 | WebView Android 51 | Chrome Android 51 | Firefox Android ? | Opera Android ? | Safari iOS 12.2 | Samsung Internet Android 5.0 |
disconnect
|
Chrome 51 |
Edge
15
|
Firefox
55
|
IE No | Opera Yes | Safari ? | WebView Android 51 | Chrome Android 51 | Firefox Android ? | Opera Android ? | Safari iOS ? | Samsung Internet Android 5.0 |
observe
|
Chrome 51 | Edge 15 |
Firefox
55
|
IE No | Opera Yes | Safari 12.1 | WebView Android 51 | Chrome Android 51 | Firefox Android ? | Opera Android ? | Safari iOS 12.2 | Samsung Internet Android 5.0 |
root
|
Chrome 51 | Edge 15 |
Firefox
55
|
IE No | Opera Yes | Safari 12.1 | WebView Android 51 | Chrome Android 51 | Firefox Android ? | Opera Android ? | Safari iOS 12.2 | Samsung Internet Android 5.0 |
rootMargin
|
Chrome 51 | Edge 15 |
Firefox
55
|
IE No | Opera Yes |
Safari
12.1
|
WebView Android 51 | Chrome Android 51 | Firefox Android ? | Opera Android ? |
Safari iOS
12.2
|
Samsung Internet Android 5.0 |
takeRecords
|
Chrome 51 |
Edge
15
|
Firefox
55
|
IE No | Opera Yes | Safari ? | WebView Android 51 | Chrome Android 51 | Firefox Android ? | Opera Android ? | Safari iOS ? | Samsung Internet Android 5.0 |
thresholds
|
Chrome 51 | Edge 15 |
Firefox
55
|
IE No | Opera Yes | Safari 12.1 | WebView Android 51 | Chrome Android 51 | Firefox Android ? | Opera Android ? | Safari iOS 12.2 | Samsung Internet Android 5.0 |
unobserve
|
Chrome 51 |
Edge
15
|
Firefox
55
|
IE No | Opera Yes | Safari 12.1 | WebView Android 51 | Chrome Android 51 | Firefox Android ? | Opera Android ? | Safari iOS 12.2 | Samsung Internet Android 5.0 |
完整支持
不支持
兼容性未知
实验。期望将来行为有所改变。
见实现注意事项。
用户必须明确启用此特征。
IntersectionObserver