--- title: IntersectionObserver slug: Web/API/IntersectionObserver translation_of: Web/API/IntersectionObserver ---
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.
IntersectionObserver
-instanssin, joka suorittaa annetun "callback" -function kun se havaitsee, että kohde-elementin näkyvyys on ylittänyt annetun kynnyksen.px
) or as a percentage (%
). The default is "0px 0px 0px 0px".IntersectionObserver
object from observing any target.IntersectionObserver
a target element to observe.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'));
Specification | Status | Comment |
---|---|---|
{{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")}}