Skip to content

Instantly share code, notes, and snippets.

@aniketchanana
Created March 25, 2023 16:54
Show Gist options
  • Save aniketchanana/69866cc2b2cbe6cc50b63bcb35596996 to your computer and use it in GitHub Desktop.
Save aniketchanana/69866cc2b2cbe6cc50b63bcb35596996 to your computer and use it in GitHub Desktop.
const element = document.querySelector(ELEMENT_SELECTOR);
const parentElement = document.querySelector(PARENT_ELEMENT_SELECTOR);
const observer = new IntersectionObserver(
(entries: any) => {
/**
* Callback will run when observed element intersect with given root
*/
},
{
root: parentElement, // if passed null take document as root
rootMargin: "0px", // give 0px margin to container value is given like CSS margin but it is not CSS
threshold: 0.5, // when 50% of the observed element is intersected
}
);
observer.observe(element);
// if you want to remove observer
observer.unobserve(element);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment