Get notified when a DOM element enters or exits the viewport. πŸ‘€
Latest commit b6389d3 Nov 16, 2016 @camwiegert 0.6.1
Failed to load latest commit information.
dist 0.6.1 Nov 16, 2016
docs 0.6.1 Nov 16, 2016
src Add MutationObserver on DOM ready Nov 16, 2016
test Add tests for test method Oct 7, 2016
.gitignore Only ignore root index for testing Aug 17, 2016
.travis.yml Add TravisCI config Aug 31, 2016
LICENSE Add license Oct 2, 2016 Add npm badge to README Oct 7, 2016
package.json 0.6.1 Nov 16, 2016
webpack.config.js Use lodash-webpack-plugin. Aug 28, 2016

in-view.js πŸ‘€

Get notified when a DOM element enters or exits the viewport. A small (~1.9kb gzipped), dependency-free, javascript utility for IE9+.

Build Status npm/in-view



Either download the latest release and include it in your markup or install with npm:

npm install --save 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.

    .on('enter', doSomething)
    .on('exit', el => { = 0.5;


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 for event. 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 for event. Handlers registered with once will only be called once.

inView('.someSelector').once('enter', doSomething);<element>)

Check if element is in the viewport.'.someSelector'));
// => true


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 least 100 pixels. offset can be a positive or negative integer.


Offset can also be set per-direction by passing an object.

    top: 100,
    right: 75,
    bottom: 50,
    left: 25


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 of 0.5 or 1 will require that half or all, respectively, of an element's height and width need to be visible. threshold must be a number between 0 and 1.



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 and false otherwise.

inView.test((el, options) => {
    // ...


Manually check the status of the elements selected by selector. By default, all registries are checked on window's scroll, resize, and load events.


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.


Any library that watches scroll events runs the risk of degrading page performance. To mitigate this, currently, in-view only registers a single, throttled (maximum once every 100ms) 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