Socket
Socket
Sign inDemoInstall

intersection-observer

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

intersection-observer

A polyfill for IntersectionObserver


Version published
Weekly downloads
1.4M
decreased by-1.81%
Maintainers
1
Weekly downloads
 
Created

What is intersection-observer?

The intersection-observer npm package is a polyfill for the Intersection Observer API, which 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. This is particularly useful for tasks like lazy loading of images or implementing 'infinite scroll' features without relying on scroll events, thereby improving performance and resource usage.

What are intersection-observer's main functionalities?

Observing visibility of an element

This code sample demonstrates how to create an IntersectionObserver to monitor when a specific element, referred to as '.target-element', becomes visible within the viewport. When the element's visibility changes to visible, a message is logged to the console.

const observer = new IntersectionObserver(entries => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      console.log('Element is visible!');
    }
  });
});

observer.observe(document.querySelector('.target-element'));

Lazy loading images

This example shows how to use IntersectionObserver for lazy loading images. Images with the class 'lazy-load' only load their source URL when they're about to enter the viewport, improving page load times. The observer stops watching an image once it has loaded.

const observer = new IntersectionObserver((entries, observer) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      entry.target.src = entry.target.dataset.src;
      observer.unobserve(entry.target);
    }
  });
}, {rootMargin: '0px', threshold: 0.1});

document.querySelectorAll('img.lazy-load').forEach(img => {
  observer.observe(img);
});

Other packages similar to intersection-observer

Keywords

FAQs

Package last updated on 10 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc