--- title: IntersectionObserver slug: Web/API/IntersectionObserver translation_of: Web/API/IntersectionObserver ---
{{APIRef("Intersection Observer API")}}

Intersection Observer API:n  rajapinta IntersectionObserver tarjoaa asynkronisen keinon tarkkailla muutoksia kohde-elementin ja sen esi-isäelementin tai ylätason dokumentin {{Glossary('viewport')}}:n kanssa. Näihin korkeamman tason DOM -elementteihin viitataan nimellä "root".

Kun IntersectionObserver luodaan, se konfiguroidaan tarkkailemaan DOM-elementtien näkyvyyttä suhteessa root:tiin. Tätä konfiguraatiota ei voida muuttaa sen jälkeen, kun se on initialisoitu, eli käytännössä yhdellä instanssilla tarkkaillaan elementtien näkyvyyttä yhden root -tason elementin suhteen. Yhdellä IntersectionObserver:lla voidaan kuitenkin tarkkailla monen elementin näkyvyyttä tämän suhteen.

Constructor

{{domxref("IntersectionObserver.IntersectionObserver()")}}
Luo uuden IntersectionObserver -instanssin, joka suorittaa annetun "callback" -function kun se havaitsee, että kohde-elementin näkyvyys on ylittänyt annetun kynnyksen.

Properties

{{domxref("IntersectionObserver.root")}} {{readonlyinline}}
Tietty elementin esi-isä -elementti, jonka suhteen kohteen {{domxref("element")}} näkyvyyttä tarkkaillaan. Jos tätä ei erityiesti määritellä, käytetään koko dokumentin juuren viewport:ia.
{{domxref("IntersectionObserver.rootMargin")}} {{readonlyinline}}
An offset rectangle applied to the root's {{Glossary('bounding box')}} when calculating intersections, effectively shrinking or growing the root for calculation purposes. The value returned by this property may not be the same as the one specified when calling the constructor as it may be changed to match internal requirements. Each offset can be expressed in pixels (px) or as a percentage (%). The default is "0px 0px 0px 0px".
{{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.
{{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);
  console.log('Loaded new items');
});
// start observing
intersectionObserver.observe(document.querySelector('.scrollerFooter'));

Specifications

Specification Status Comment
{{SpecName("IntersectionObserver", "#intersection-observer-interface", "IntersectionObserver")}} {{Spec2('IntersectionObserver')}}

Browser compatibility

{{Compat("api.IntersectionObserver")}}

See also