Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntersectionObserver callback firing immediately on page load

I'm very new to the IntersectionObserver API, and I've been experimenting with this code:

let target = document.querySelector('.lazy-load');  let options = {     root: null,     rootMargin: '0px',     threshold: 0 }  let observer = new IntersectionObserver(callback, options);  observer.observe(target);  function callback() {     console.log('observer triggered.'); } 

This seems to work as it should, and callback() is called whenever .lazy-load element enters the viewport, but callback() also fires once when the page is initially loaded, which triggers `console.log('observer triggered.');

Is there a reason for this callback to be triggered when the page loads? Or is there a mistake in how I'm implementing this?

Edit: Altering the code to the below still fires the callback at page load.

let target = document.querySelector('.lazy-load');  let options = {     root: null,     rootMargin: '0px',     threshold: 0 }  let callback = function(entries, observer) {     entries.forEach(entry => {          console.log('observer triggered.');      }); };  let observer = new IntersectionObserver(callback, options);  observer.observe(target); 
like image 853
rpivovar Avatar asked Nov 08 '18 18:11

rpivovar


People also ask

What is threshold in IntersectionObserver?

Thresholds. Rather than reporting every infinitesimal change in how much a target element is visible, the Intersection Observer API uses thresholds. When you create an observer, you can provide one or more numeric values representing percentages of the target element which are visible.

What is IntersectionObserver in Javascript?

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

What does intersection observer mean?

Chrome for Android? Android WebView? Samsung Internet? Opera Mobile? The IntersectionObserver interface can be used to observe changes in the intersection of an intersection root and one or more target Element s.

What is rootMargin in intersection observer?

The IntersectionObserver interface's read-only rootMargin property is a string with syntax similar to that of the CSS margin property. Each side of the rectangle represented by rootMargin is added to the corresponding side in the root element's bounding box before the intersection test is performed.


1 Answers

That is the default behaviour. When you instantiate an instance of the IntersectionObserver, the callback will be fired.

It is recommended to guard against this case.

entries.forEach(entry => {   if (entry.intersectionRatio > 0) {     entry.target.classList.add('in-viewport');   } else {     entry.target.classList.remove('in-viewport');   } }); 

Also I found this article as well as the docs to be very helpful, specifically about the intersectionRatio or isIntersecting properties on the IntersectionObserverEntry.

· https://www.smashingmagazine.com/2018/01/deferring-lazy-loading-intersection-observer-api/

· https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver

· https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry

like image 93
snewcomer Avatar answered Sep 22 '22 14:09

snewcomer