Intersection Observer
intersectionObserver
gives you an incredibly simple way to use the Intersection Observer API — so much that you’ll probably wanna ditch the original API for good.
Super simple way to use Intersection Observer
Instead of writing the standard Intersection Observer boilerplate, just use our utility function.
It can’t get simpler than doing the stuff you want inside a standard callback
!
const node = document.querySelector('.my-element')const obs = intersectionObserver(node, { callback({ entry }) { // Do something with the intersection observer },})
Listen to multiple elements at once
Grab the elements — either as an array or nodelist — and pass them to intersectionObserver
. It’ll listen to them without complains.
const nodes = document.querySelectorAll('.elements')const obs = intersectionObserver(nodes, { callback({ entry }) { // Do something with the intersection observer },})
Unobserve or disconnect whenever you wish to
We support all the intersection observer methods you’d expect — check out the guide for more details.
const obs = resizeObserver(node, /* ... */)
// Stops listening to the elementobs.unobserve(node)
Automatically take records before disconnecting
Intersection Observer has a takeRecords
method that lets you process unprocessed intersections before disconnecting.
We’ve built that directly into the disconnect
method so you never have to worry about taking records manually.
const obs = intersectionObserver(node, /* ... */)
// Stops listening.// No need to call `takeRecords` manually!obs.disconnect()
Basic Usage
Import the intersectionObserver
function from @splendidlabz/utils/dom
.
import { intersectionObserver } from '@splendidlabz/utils/dom'
Grab the element you want to listen to, and pass that into intersectionObserver
. Three properties will be available in the callback:
entries
— a list of all observed entriesentry
— the current entryobserver
— theintersectionObserver
instance
const node = document.querySelector('.my-element')const observer = intersectionObserver(node, { callback({ entries, entry, observer }) { // Do something with the resize observer },})
Observing additional elements
You can observe additional elements after the initial setup with the observe
method.
const observer = intersectionObserver(node, /* ... */)
// Observe additional elementsconst node2 = document.querySelector('.my-element-2')observer.observe(node2)
Listening to multiple elements
Grab the elements you want to listen to — either as an array or nodelist — and pass them to intersectionObserver
. It’ll listen to them all.
const nodes = document.querySelectorAll('.elements')const observer = intersectionObserver(nodes, { callback({ entry }) { // Do something with the resize observer },})
Event Listener Pattern
You can also listen for intersectionObserver
changes via the event listener pattern. Just listen to the mutate
event.
const observer = intersectionObserver(node, /* ... */)
node.addEventListener('mutate', ({ detail }) => { const { entries, entry, observer } = detail // Do something with the resize observer})
Observer Options
You can pass in all intersectionObserver
options to intersectionObserver
via the second argument.
const observer = intersectionObserver(node, { callback({ entry }) { /* ... */ }, // Other options go here ...})
IntersectionObserver takes in three options:
root
: The element used to check if observed elements are visiblerootMargin
: Lets you specify an offset amount from the edges of the rootthreshold
: Determines when to log an observer entry
Stop observing
Stop observing one element
To stop observing one element, use the unobserve
method.
const observer = intersectionObserver(nodes, /* ... */)observer.unobserve(node)
Stop observing multiple elements
To stop observing multiple elements, you can pass the elements to unobserve to the unobserve
method.
const observer = intersectionObserver(nodes, /* ... */)observer.unobserve(nodes)
Alternatively, you can use the disconnect
method to stop observing all elements.
Disconnect the observer
Disconnects the intersection observer and stops observing all elements.
While disconnecting, intersectionObserver
will automatically call takeRecords
to process any unprocessed intersections.
const obs = intersectionObserver(nodes, /* ... */)
// Disconnects the observerobs.disconnect()
Please purchase a license to view this content
This content is part of our premium documentation. To access it, you'll need to have an active license for one of these products.
If you already have a license, please login to access this content.
Basic Usage
Import the intersectionObserver
function from @splendidlabz/utils/dom
.
import { intersectionObserver } from '@splendidlabz/utils/dom'
Grab the element you want to listen to, and pass that into intersectionObserver
. Three properties will be available in the callback:
entries
— a list of all observed entriesentry
— the current entryobserver
— theintersectionObserver
instance
const node = document.querySelector('.my-element')const observer = intersectionObserver(node, { callback({ entries, entry, observer }) { // Do something with the resize observer },})
Observing additional elements
You can observe additional elements after the initial setup with the observe
method.
const observer = intersectionObserver(node, /* ... */)
// Observe additional elementsconst node2 = document.querySelector('.my-element-2')observer.observe(node2)
Listening to multiple elements
Grab the elements you want to listen to — either as an array or nodelist — and pass them to intersectionObserver
. It’ll listen to them all.
const nodes = document.querySelectorAll('.elements')const observer = intersectionObserver(nodes, { callback({ entry }) { // Do something with the resize observer },})
Event Listener Pattern
You can also listen for intersectionObserver
changes via the event listener pattern. Just listen to the mutate
event.
const observer = intersectionObserver(node, /* ... */)
node.addEventListener('mutate', ({ detail }) => { const { entries, entry, observer } = detail // Do something with the resize observer})
Observer Options
You can pass in all intersectionObserver
options to intersectionObserver
via the second argument.
const observer = intersectionObserver(node, { callback({ entry }) { /* ... */ }, // Other options go here ...})
IntersectionObserver takes in three options:
root
: The element used to check if observed elements are visiblerootMargin
: Lets you specify an offset amount from the edges of the rootthreshold
: Determines when to log an observer entry
Stop observing
Stop observing one element
To stop observing one element, use the unobserve
method.
const observer = intersectionObserver(nodes, /* ... */)observer.unobserve(node)
Stop observing multiple elements
To stop observing multiple elements, you can pass the elements to unobserve to the unobserve
method.
const observer = intersectionObserver(nodes, /* ... */)observer.unobserve(nodes)
Alternatively, you can use the disconnect
method to stop observing all elements.
Disconnect the observer
Disconnects the intersection observer and stops observing all elements.
While disconnecting, intersectionObserver
will automatically call takeRecords
to process any unprocessed intersections.
const obs = intersectionObserver(nodes, /* ... */)
// Disconnects the observerobs.disconnect()
Intersection Observer API
The intersectionObserver
function creates an IntersectionObserver
instance that watches for when elements enter or exit the viewport.
const observer = intersectionObserver(target, options)
Parameters
Parameter | Type | Description |
---|---|---|
target | Element Element[] NodeList | The element(s) to observe |
options | object | Configuration options for the observer |
Options
Option | Type | Default | Description |
---|---|---|---|
callback | function | - | Function called when intersection changes occur. Receives { entry, entries, observer } |
root | Element | null | The element that is used as the viewport for checking visibility |
rootMargin | string | "0px" | Margin around the root. Can have values similar to the CSS margin property |
threshold | number number[] | 0 | Either a single number or an array of numbers between 0.0 and 1.0, specifying a ratio of intersection area to total bounding box area |
For more details about these options, see the MDN documentation.
Callback Parameters
When an intersection change occurs, the callback receives an object with:
Property | Type | Description |
---|---|---|
entry | IntersectionObserverEntry | The current intersection entry being processed |
entries | IntersectionObserverEntry[] | All intersection entries being observed |
observer | IntersectionObserver | The observer instance |
Return Value
Returns an object with the following methods:
Method | Parameters | Return Value | Description |
---|---|---|---|
observe | Element Element[] NodeList | - | Starts observing new elements for intersection changes |
unobserve | Element Element[] NodeList | - | Stops observing elements for intersection changes |
takeRecords | - | IntersectionObserverEntry[] | Returns all pending intersection entries and clears the queue |
disconnect | - | - | Stops observing all elements, processes any pending entries through the callback, and then disconnects the observer |
destroy | - | - | Alias for disconnect() |
Event Listener Alternative
If no callback is provided, the observer will dispatch an intersect
event on the target element. You can listen for this event:
element.addEventListener('intersect', ({ detail }) => { const { entry, entries, observer } = detail // Handle intersection change})
Please purchase a license to view this content
This content is part of our premium documentation. To access it, you'll need to have an active license for one of these products.
If you already have a license, please login to access this content.
Intersection Observer API
The intersectionObserver
function creates an IntersectionObserver
instance that watches for when elements enter or exit the viewport.
const observer = intersectionObserver(target, options)
Parameters
Parameter | Type | Description |
---|---|---|
target | Element Element[] NodeList | The element(s) to observe |
options | object | Configuration options for the observer |
Options
Option | Type | Default | Description |
---|---|---|---|
callback | function | - | Function called when intersection changes occur. Receives { entry, entries, observer } |
root | Element | null | The element that is used as the viewport for checking visibility |
rootMargin | string | "0px" | Margin around the root. Can have values similar to the CSS margin property |
threshold | number number[] | 0 | Either a single number or an array of numbers between 0.0 and 1.0, specifying a ratio of intersection area to total bounding box area |
For more details about these options, see the MDN documentation.
Callback Parameters
When an intersection change occurs, the callback receives an object with:
Property | Type | Description |
---|---|---|
entry | IntersectionObserverEntry | The current intersection entry being processed |
entries | IntersectionObserverEntry[] | All intersection entries being observed |
observer | IntersectionObserver | The observer instance |
Return Value
Returns an object with the following methods:
Method | Parameters | Return Value | Description |
---|---|---|---|
observe | Element Element[] NodeList | - | Starts observing new elements for intersection changes |
unobserve | Element Element[] NodeList | - | Stops observing elements for intersection changes |
takeRecords | - | IntersectionObserverEntry[] | Returns all pending intersection entries and clears the queue |
disconnect | - | - | Stops observing all elements, processes any pending entries through the callback, and then disconnects the observer |
destroy | - | - | Alias for disconnect() |
Event Listener Alternative
If no callback is provided, the observer will dispatch an intersect
event on the target element. You can listen for this event:
element.addEventListener('intersect', ({ detail }) => { const { entry, entries, observer } = detail // Handle intersection change})