Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1113069 of Intersection Observer API

  • Revision slug: Web/API/Intersection_Observer_API
  • Revision title: Intersection Observer API
  • Revision id: 1113069
  • Created:
  • Creator: paul.irish
  • Is current revision? No
  • Comment

Revision Content

{{ SeeCompatTable() }}

The Intersection Observer 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 {{Glossary("viewport")}}.

Intersection observer concepts and usage

The Intersection Observer API allows you to configure a callback that is called whenever one item, called a target, intersects either the device viewport or a specified element called, for the purpose of this API, the root element. Create the intersection observer by calling its constructor and passing it a reference to the callback function.

var options = {
    root: document.querySelector('#scrollArea'), 
    rootMargin: '0px', 
    threshold: 1.0
}
var callback = function(entries, observer) { 
    /* Content excerpted, show below */ 
};
var observer = new IntersectionObserver(callback, options);

A threshold of 1.0 means that when 100 percent of target is visible within the root element, the callback is invoked.

Once you have the observer, give it a target.

var target = document.querySelector('#listItem');
observer.observe(target);

Whenever the target meets the threshold specified for the IntersectionObserver, the callback is invoked.

var callback = function(entries, observer) { 
    entries.forEach(entry => {
        entry.time;               // a DOMHightResTimeStamp indicating when the intersection occurred.
        entry.rootBounds;         // a DOMRectReadOnly for the intersection observer's root.
        entry.boundingClientRect; // a DOMRectReadOnly for the intersection observer's target.
        entry.intersectionRect;   // a DOMRectReadOnly for the visible portion of the intersection observer's target.
        entry.intersectionRatio;  // the number for the ratio of the intersectionRect to the boundingClientRect.
        entry.target;             // the Element whose intersection with the intersection root changed.
    });
};

Interfaces

{{domxref("IntersectionObserver")}}
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 {{Glossary('viewport')}}. The ancestor or viewport is referred to as the root.
{{domxref("IntersectionObserverEntry")}}
Provides information about the intersection of a particular target with the observers root element at a particular time. Instances of this interface cannot be created, but a list of them is returned by {{domxref("IntersectionObserver.takeRecords", "IntersectionObserver.takeRecords()")}}.

Specifications

Specification Status Comment
{{SpecName('IntersectionObserver')}} {{Spec2('IntersectionObserver')}} Initial definition.

Browser Compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support

{{CompatChrome(51.0)}}

{{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatNo}} {{CompatChrome(51.0)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatChrome(51.0)}}

Revision Source

<div>
<p>{{ SeeCompatTable() }}</p>

<p class="summary">The Intersection Observer 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 {{Glossary("viewport")}}.</p>
</div>

<h2 id="Intersection_observer_concepts_and_usage">Intersection observer concepts and usage</h2>

<p>The Intersection Observer API allows you to configure a callback that is called whenever one item, called a target, intersects either the device viewport or a specified element called, for the purpose of this API, the root element. Create the intersection observer by calling its constructor and passing it a reference to the callback function.</p>

<pre class="brush: js">
var options = {
&nbsp;&nbsp; &nbsp;root: document.querySelector('#scrollArea'),&nbsp;
&nbsp;&nbsp; &nbsp;rootMargin: '0px',&nbsp;
&nbsp;&nbsp; &nbsp;threshold: 1.0
}
var callback = function(entries, observer) {&nbsp;
&nbsp;&nbsp; &nbsp;/* Content excerpted, show below */&nbsp;
};
var observer = new IntersectionObserver(callback, options);</pre>

<p>A threshold of 1.0 means that when&nbsp;100 percent of <code>target</code> is visible within the&nbsp;<code>root </code>element, the callback is invoked.</p>

<p>Once you have the observer, give it a target.</p>

<pre class="brush: js">
var target = document.querySelector('#listItem');
observer.observe(target);
</pre>

<p>Whenever the target meets the threshold specified for the IntersectionObserver, the callback is invoked.</p>

<pre class="brush: js">
var callback = function(entries, observer) {&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;entries.forEach(entry =&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;entry.time; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // a DOMHightResTimeStamp indicating when the intersection occurred.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;entry.rootBounds; &nbsp; &nbsp; &nbsp; &nbsp; // a DOMRectReadOnly for the intersection observer's root.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;entry.boundingClientRect; // a&nbsp;DOMRectReadOnly&nbsp;for&nbsp;the intersection observer's target.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;entry.intersectionRect; &nbsp; // a DOMRectReadOnly for the visible portion of the intersection observer's target.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;entry.intersectionRatio; &nbsp;// the number for the ratio of the&nbsp;intersectionRect&nbsp;to the&nbsp;boundingClientRect.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;entry.target; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the Element whose intersection with the intersection root changed.
&nbsp;&nbsp;&nbsp;&nbsp;});
};
</pre>

<h2 id="Interfaces">Interfaces</h2>

<dl>
 <dt>{{domxref("IntersectionObserver")}}</dt>
 <dd>Provides a way to&nbsp;<span style="line-height:1.5">asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's {{Glossary('viewport')}}. The ancestor or viewport is referred to as the root.</span></dd>
 <dt>{{domxref("IntersectionObserverEntry")}}</dt>
 <dd>Provides information about the intersection of a particular target with the observers root element at a particular time. Instances of this interface cannot be created, but a list of them is returned by {{domxref("IntersectionObserver.takeRecords", "IntersectionObserver.takeRecords()")}}.</dd>
</dl>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('IntersectionObserver')}}</td>
   <td>{{Spec2('IntersectionObserver')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_Compatibility">Browser Compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>
    <p>{{CompatChrome(51.0)}}</p>
   </td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Firefox OS</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(51.0)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatChrome(51.0)}}</td>
  </tr>
 </tbody>
</table>
</div>
Revert to this revision