--- title: Intersection Observer slug: Web/API/IntersectionObserver tags: - API - Experimental - Interface - Intersection Observer API - IntersectionObserver - Reference - observers translation_of: Web/API/IntersectionObserver ---
IntersectionObserver
接口 (从属于Intersection Observer API) 提供了一种异步观察目标元素与其祖先元素或顶级文档视窗({{Glossary('viewport')}})交叉状态的方法。祖先元素与视窗({{Glossary('viewport')}})被称为根(root)。
当一个IntersectionObserver
对象被创建时,其被配置为监听根中一段给定比例的可见区域。一旦IntersectionObserver被创建,则无法更改其配置,所以一个给定的观察者对象只能用来监听可见区域的特定变化值;然而,你可以在同一个观察者对象中配置监听多个目标元素。
IntersectionObserver
对象,当其监听到目标元素的可见部分穿过了一个或多个阈(thresholds)时,会执行指定的回调函数。null
,则默认使用顶级文档的视窗。px
)或百分比(percentage)(%
)来表达, 默认值为"0px 0px 0px 0px"。IntersectionObserver
对象停止监听工作。IntersectionObserver
开始监听一个目标元素。IntersectionObserver
停止监听特定目标元素。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'));
规范 | 状态 | 备注 |
---|---|---|
{{SpecName("IntersectionObserver", "#intersection-observer-interface", "IntersectionObserver")}} | {{Spec2('IntersectionObserver')}} |
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.
{{Compat("api.IntersectionObserver")}}