@soldotno/aller-in-view
v3.0.2
Published
Get notified when a DOM element enters or exits the viewport.
Downloads
830
Keywords
Readme
in-view.js :eyes:
Get notified when a DOM element enters or exits the viewport. A small almost dependency-free, javascript utility for IE9+.
Based on kudago-in-view that is based on Based on camwiegert.github.io/in-view
This is a fork, you should check out if the original has all the usecases you need before using this.
Installation
You can install it with npm:
npm install --save aller-in-view
Basic Usage
With in-view, you can register handlers that are called when an element enters or exits the viewport. Each handler receives one element, the one entering or exiting the viewport, as its only argument.
inView('.someSelector')
.on('enter', doSomething)
.on('exit', el => {
el.style.opacity = 0.5;
});
API
in-view maintains a separate handler registry for each set of elements captured with inView(<selector>)
. Each registry exposes the same four methods. in-view also exposes four top-level methods. (is
, offset
, threshold
, test
).
inView(<selector>).on(<event>, <handler>)
Register a handler to the elements selected by
selector
forevent
. The only events in-view emits are'enter'
and'exit'
.
inView('.someSelector').on('enter', doSomething);
inView(<selector>).once(<event>, <handler>)
Register a handler to the elements selected by
selector
forevent
. Handlers registered withonce
will only be called once.
inView('.someSelector').once('enter', doSomething);
inView(<selector>).off(<event>, <handler>)
Unregister a handler to the elements selected by
selector
forevent
. Handler is a function instance.
inView('.someSelector').off('enter', doSomething);
inView.is(<element>)
Check if
element
is in the viewport.
inView.is(document.querySelector('.someSelector')); // => true
inView.addEventOptions(<options>)
If you are not going to cancel the event, you can get a perfomance gain by adding
preventDefault
. https://developers.google.com/web/updates/2016/06/passive-event-listeners OBS: must before any call to inViewimport inViewFactory from 'in-view'; inView = inView(); const options = { capture: true, once: false, passive:true } inView.addEventOptions(options); // inView.addEventOptions must be called before the first call to inView constructor inView("div")
inView.interval(<newIntervalTime>)
If you want to change the throttled time on how often inView checks the elements OBS: must before any call to inView, see example above
inView.interval(200); // default is 100
inView.offset(<offset>)
By default, in-view considers something in viewport if it breaks any edge of the viewport. This can be used to set an offset from that edge. For example, an offset of
100
will consider elements in viewport if they break any edge of the viewport by at least100
pixels.offset
can be a positive or negative integer.
inView.offset(100); inView.offset(-50);
Offset can also be set per-direction by passing an object.
inView.offset({ top: 100, right: 75, bottom: 50, left: 25 });
inView.threshold(<threshold>)
Set the ratio of an element's height and width that needs to be visible for it to be considered in viewport. This defaults to
0
, meaning any amount. A threshold of0.5
or1
will require that half or all, respectively, of an element's height and width need to be visible.threshold
must be a number between0
and1
.inView.threshold(0); inView.threshold(0.5); inView.threshold(1);
inView.runExitOnElementsCurrentlyInView()
runs a check on all the elements currently inView (not in viewport, but based on the criterias given in threshold and offset) and runs exit on them. Can for example be used to send data to an analytics platform on the onunload event
inView.runExitOnElementsCurrentlyInView()
inView.test(<test>)
Override in-view's default visibility criteria with a custom function. This function will receive the element and the options object as its only two arguments. Return
true
when an element should be considered visible andfalse
otherwise.inView.test((el, options) => { // ... });
inView(<selector>).check()
Manually check the status of the elements selected by
selector
. By default, all registries are checked onwindow
'sscroll
,resize
, andload
events.
inView('.someSelector').check();
inView(<selector>).emit(<event>, <element>)
Manually emit
event
for any single element.
inView('.someSelector').emit('exit', document.querySelectorAll('.someSelector')[0]);
Browser Support
in-view supports all modern browsers and IE9+.
As a small caveat, in-view utilizes MutationObserver to check the visibility of registered elements after a DOM mutation. If that's functionality you need in IE9-10, consider using a polyfill.
Performance
Any library that watches scroll events runs the risk of degrading page performance. To mitigate this, currently, in-view only registers a single, throttled (can be customized) event listener on each of window
's load
, resize
, and scroll
events and uses those to run a check on each registry.
Utilizing IntersectionObserver
There's an emerging browser API, IntersectionObserver
, that aims to provide developers with a performant way to check the visibility of DOM elements. Going forward, in-view will aim to delegate to IntersectionObserver
when it's supported, falling back to polling only when necessary.
License MIT