A simple, efficient, and lightweight ES module to monitor the DOM for when elements are added, removed, have appeared, have disappeared, or are resized. Internally, this library uses the MutationObserver, IntersectionObserver, and ResizeObserver APIs.
;const monitor = ;// watch for new elements added to the DOMmonitor;// watch for elements removed from the DOMmonitor;// watch for elements to become appear on the page// what it means to appear or be visible is complicated, details here://monitor;// watch for elements that are no longer visible on the pagemonitor;// watch for when elements are resizedmonitor;
Add to your project using NPM:
$ npm install monitoring --save
You can add
monitoring directly in your site or download the latest minified version from jsdelivr:
Getting callback details
Callbacks also recieve the observer entry that triggered the callback. This table shows the type of entry each methods recieves:
Here are some examples of how the entry information can be used:
const monitor = ;monitor;monitor;monitor;
You can cancel a monitor by calling its
cancel method. This cancels all callbacks registered against that monitor:
const monitor = ;const divAdded = monitor;const divRemoved = monitor;// stops the monitor, including divAdded and divRemovedmonitor;
You can also cancel a specific callback:
const monitor = ;const divAdded = monitor;const divRemoved = monitor;// cancels divAdded, doesn't effect divRemoveddivAdded;
Finally, you can cancel by returning
false from within the callback. Note: Your callback must return
false, not a falsey value like
const monitor = ;// cancels the callback after its first callmonitor;
Monitors support an
iframes option to include monitoring elements within iframes of the same origin. For example:
const monitor = ;monitor;
By default, the
added method will return all existing elements in the DOM that match the given selector and monitor for new ones. You can ignore existing elements by setting the
existing option to
const monitor = ;monitor;
Monitors reuse their observers so you should avoid declaring new monitors for the same element. For example:
// this only uses one monitor for two callback - do this :-)const monitor = ;monitor
Observers were designed to be an efficient alternative to polling the DOM. However, monitoring large chunks of the DOM, like
document.body is still expensive. I recommend monitoring the smallest portion of the DOM necessary and cancelling as soon as the monitor is no longer needed.
Here is an example of using a monitor to find more specific elements for monitoring:
// monitor the document body for a #content div;
monitoring different from
arrive.js is an excellent library and was the inspiration for this project. However, there are some differences:
arrive.jsdoes not support the
arrive.jsdoes not support traversing into iframes.
arrive.jsis not an ES6 module, which makes it difficult to incorporate into things like webpack.
arrive.jspollutes the DOM by decorating all matched elements and a number of prototypes.
arrive.jsuses recursive node matching, which can be slow.
arrivecreates a new
MutationObserverfor each callback, which can be slow.
arrive.jsrequires jquery for observing elements from different documents. For example:
// works!documentbody;// does not work :-(frames0documentbody;// works, but you need jquery;