✨ A small IntersectionObserver
wrapper helping to detect easily DOM elements entering or leaving the viewport.
cartapus is a library designed to help you manage detection of elements in the browser's viewport.
The goal of cartapus is to provide a quick and easy to use solution to those who need to animate/manipulate elements when they appear/disappear from the screen, using the IntersectionObserver
API.
Cartapus also watches DOM modifications to start observing newly added elements automatically (and also to stop observing freshly removed element) using the MutationObserver
API.
$ npm i cartapus
Initialize the library in your codebase :
import Cartapus from 'cartapus'
const cartapus = new Cartapus()
Now, add data-cartapus
attribute to any element you need to observe :
<div class="box" data-cartapus></div>
When this element will be visible in the viewport, its attribute will switch to data-cartapus="visible"
. Allowing you to adapt your CSS to easily animate this element.
An invisible element will have its attribute set to data-cartapus="hidden"
.
Here is a fade in example :
.box[data-cartapus]{
/* HIDDEN STATE */
opacity: 0;
transition: opacity 1s;
}
.box[data-cartapus=visible]{
/* VISIBLE STATE */
opacity: 1;
}
Here are all the available options :
const cartapus = new Cartapus({
root: null,
rootMargin: '0px',
threshold: 0,
once: false
})
root
,rootMargin
andthreshold
are all three related to theIntersectionObserver
API. You can have more information about these parameters by consulting the official documentation on MDN.
Option | Type | Default | Description |
---|---|---|---|
root | Element | null (entire viewport) |
The root DOM element into which [data-cartapus] targets will be observed. Default is set to null , which is equivalent to the entire viewport. (More information here) |
rootMargin | string | '0px' |
A CSS margin property string defining offsets into the root element. (More information here) |
threshold | number | 0 |
A number between 0 and 1 which defines the percentage of height that must be into the viewport for an element to be considered "visible". (More information here) |
once | boolean | false |
If true , elements that are visible will never return to their hidden state even if they disappear from the root . |
Cartapus will trigger events when an element changes its state to visible
or hidden
.
Events are called for all elements immediately after initializing Cartapus, providing you their initial visibility state.
To listen to the events, use the following methods :
// Watch intersections instance-wide
cartapus.on('intersect', ({ element, visible, intersection }) => {
// `element` just switched its visibility
})
// Watch intersection per element
el.addEventListener('cartapusintersect', ({ detail }) => {
// `detail.element` just switched its visibility
})
Some parameters are given to these callbacks :
element
: TheElement
that triggered the event.visible
: Whether the element is visible or not (same as itsdata-cartapus
value).intersection
: TheIntersectionObserverEntry
instance.
detail
contains exactly the same properties as given in the instance event (element
,visible
andintersection
).
Maybe you want a specific element to switch its state with a different threshold than the others.
Or you may want to prevent a specific element from going back to its hidden
state (triggering the event only once).
Some additional attributes are available to allow both of those cases, overriding the default options for this specific element :
<div data-cartapus
data-cartapus-threshold="0.5"
data-cartapus-root-margin="0px 0px -200px 0px"
data-cartapus-once>
</div>
data-cartapus-threshold
: overrides thethreshold
option. Ie : this element will be visible when 50% of its height is visible.data-cartapus-root-margin
: overrides therootMargin
option. Ie : the bottom bounding box of the viewport will be shrunk by 200px on the bottom, as if the viewport was smaller.data-cartapus-once
: overrides theonce
option. Ie : this element will switch tovisible
, then never switch back tohidden
again. To explicitly turn off theonce
option, use it like this :data-cartapus-once="false"
.
Some methods are available, to turn on/off Cartapus programmatically :
Cartapus watches DOM changes and observes automatically appended elements. But in some cases you may need to start observing an element manually (when a data-cartapus
attribute has been added after the element being appended, etc).
This method returns true
if the element is now being watched, false
if not.
Triggers instantly the cartapus events related to the given elements (can be an array of elements, or a single element).
If targets
parameter is not given, it triggers the events of all observed elements.
Stops observing all [data-cartapus]
elements. And disconnects all the IntersectionObservers
along with the MutationObserver
.
Stops observing all [data-cartapus]
elements.
Starts observing all [data-cartapus]
elements.
This method triggers instantly the
events
for every element.
Cartapus supports all recent major versions of the modern browsers.
Internally, Cartapus uses the IntersectionObserver
and MutationObserver
APIs to observe elements. You can have more details about compatibility by consulting CanIuse :
"On me voit. On me voit plus. On me voit un peu, on me voit plus." - Cartapus (2002)