Keep reading for a more detailed explanation about intersection observer options. To watch for intersection relative to the device's viewport, specify null for root option. Typically, you'll want to watch for intersection changes with regard to the target element's closest scrollable ancestor, or, if the target element isn't a descendant of a scrollable element, the device's viewport.
![definition of observation definition of observation](https://cdn.slidesharecdn.com/ss_thumbnails/observation-160613130331-thumbnail-4.jpg)
#DEFINITION OF OBSERVATION FREE#
This way, sites no longer need to do anything on the main thread to watch for this kind of element intersection, and the browser is free to optimize the management of intersections as it sees fit.
#DEFINITION OF OBSERVATION CODE#
The Intersection Observer API lets code register a callback function that is executed whenever an element they wish to monitor enters or exits another element (or the viewport), or when the amount by which the two intersect changes by a requested amount. As the user scrolls the page, these intersection detection routines are firing constantly during the scroll handling code, resulting in an experience that leaves the user frustrated with the browser, the web site, and their computer.
![definition of observation definition of observation](https://image.slidesharecdn.com/ch03-160222222359/95/ch03-9-638.jpg)
The author of the web site may not even realize this is happening, since they may know very little about the inner workings of the two libraries they are using. Each of these has its own intersection detection routines, all running on the main thread. It uses a vendor-provided library to manage the advertisements placed periodically throughout the page, has animated graphics here and there, and uses a custom library that draws notification boxes and the like.
![definition of observation definition of observation](http://image.slidesharecdn.com/observations-vs-inferences2071/95/observations-vs-inferences-4-728.jpg)
When a site is loaded with these tests, things can get downright ugly.Ĭonsider a web page that uses infinite scrolling. Since all this code runs on the main thread, even one of these can cause performance problems. Implementing intersection detection in the past involved event handlers and loops calling methods like Element.getBoundingClientRect() to build up the needed information for every element affected.
![definition of observation definition of observation](https://image.slidesharecdn.com/inference-130925194641-phpapp02/95/inference-5-638.jpg)
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 viewport.