Super simple way to use Resize Observer
Instead of writing the standard ResizeObserver
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 = resizeObserver(node, { callback({ entry }) { // Do something with the resize observer },})
Listen to multiple elements at once
Grab the elements — either as an array or nodelist — and pass them to resizeObserver
. It’ll listen to them without complains.
const nodes = document.querySelectorAll('.elements')const obs = resizeObserver(nodes, { callback({ entry }) { // Do something with the resize observer },})
Unobserve or disconnect whenever you wish to
We support all the resize observer methods you’d expect — check out the guide for more details.
const obs = resizeObserver(node, /* ... */)
// Stops listening to the elementobs.unobserve(node)