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 1077546 of IntersectionObserver

  • Revision slug: Web/API/IntersectionObserver
  • Revision title: IntersectionObserver
  • Revision id: 1077546
  • Created:
  • Creator: jpmedley
  • Is current revision? No
  • Comment Add to property description.

Revision Content

{{SeeCompatTable}}{{APIRef("")}}

The IntersectionObserver interface of the 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')}}. The ancestor or viewport is referred to as the root.

Constructor

{{domxref("IntersectionObserver.IntersectionObserver()")}}
Creates a new IntersectionObserver object.

Properties

{{domxref("IntersectionObserver.root")}} {{readonlyinline}}
A specific ancestor of the {{domxref("element")}} being observed. If no value was passed to the constructor, the top-level document's viewport is used.
{{domxref("IntersectionObserver.rootMargin")}} {{readonlyinline}}
An offset applied to the root's {{Glossary('bounding_box')}} when calculating intersections, effectively shrinking or growing the root for calculation purposes. This is not guaranteed to be identical to the value passed in the constructor. If no value was passed to the constructor, "0px 0px 0px 0px" is used. This property takes pixels ('px') or percentages ('%').
{{domxref("IntersectionObserver.thresholds")}} {{readonlyinline}}
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.

Methods

{{domxref("IntersectionObserver.disconnect")}}
Stops the IntersectionObserver object from observing any target.
{{domxref("IntersectionObserver.observe")}}
Tells the IntersectionObserver a target element to observe.
{{domxref("IntersectionObserver.takeRecords")}}
Returns an array of {{domxref("IntersectionObserverEntry")}} objects for all observed targets and stops observing all of them.
{{domxref("IntersectionObserver.unobserve")}}
Tells the IntersectionObserver to stop observing a particular target element.

Examples

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);
  // appendChild will move the existing element, so there is no need to
  // remove it first.
  scroller.appendChild(target);
  loadItems(5);
  ChromeSamples.setStatus('Loaded up to item ' + counter);
});
intersectionObserver.observe(target);

Specifications

Specification Status Comment
{{SpecName('IntersectionObserver','#intersection-observer-private-slots','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

<p>{{SeeCompatTable}}{{APIRef("")}}</p>

<p>The <code><strong>IntersectionObserver</strong></code> interface of the the <a href="/en-US/docs/Web/API/Intersection_Observer_API">Intersection Observer API</a> 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.</p>

<h2 id="Constructor">Constructor</h2>

<dl>
 <dt>{{domxref("IntersectionObserver.IntersectionObserver()")}}</dt>
 <dd>Creates a new <code>IntersectionObserver</code> object.</dd>
</dl>

<h2 id="Properties">Properties</h2>

<dl>
 <dt>{{domxref("IntersectionObserver.root")}} {{readonlyinline}}</dt>
 <dd>A specific ancestor of the {{domxref("element")}} being observed. If no value was&nbsp;passed to the constructor, the top-level document's viewport is used.</dd>
 <dt>{{domxref("IntersectionObserver.rootMargin")}} {{readonlyinline}}</dt>
 <dd>An offset applied to the root's {{Glossary('bounding_box')}} when calculating intersections, effectively shrinking or growing the root for calculation purposes.&nbsp;This is not guaranteed to be identical to the value passed in the constructor. If no value was&nbsp;passed to the constructor, "0px 0px 0px 0px" is used. This property takes pixels ('px') or percentages ('%').</dd>
 <dt>{{domxref("IntersectionObserver.thresholds")}} {{readonlyinline}}</dt>
 <dd>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.</dd>
</dl>

<h2 id="Methods">Methods</h2>

<dl>
 <dt>{{domxref("IntersectionObserver.disconnect")}}</dt>
 <dd>Stops the IntersectionObserver object from observing any target.</dd>
 <dt>{{domxref("IntersectionObserver.observe")}}</dt>
 <dd>Tells the <code>IntersectionObserver</code> a target element to observe.</dd>
 <dt>{{domxref("IntersectionObserver.takeRecords")}}</dt>
 <dd>Returns an array of {{domxref("IntersectionObserverEntry")}} objects for&nbsp;all observed targets and stops observing&nbsp;all of them.</dd>
 <dt>{{domxref("IntersectionObserver.unobserve")}}</dt>
 <dd>Tells the <code>IntersectionObserver</code> to stop observing a particular target element.</dd>
 <dt>
 <h2 id="Examples">Examples</h2>
 </dt>
</dl>

<pre class="brush: js">
<code data-lang="js">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 &lt;= 0) {
    return;
  }
  loadItems(10);
  // appendChild will move the existing element, so there is no need to
  // remove it first.
  scroller.appendChild(target);
  loadItems(5);
  ChromeSamples.setStatus('Loaded up to item ' + counter);
});
intersectionObserver.observe(target);</code></pre>

<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','#intersection-observer-private-slots','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