in-viewport

Get a callback when an element enters the viewport (body) or a custom viewport

inViewport

Know when an element is in the window viewport or a custom viewport.

var inViewport = require('in-viewport');
var elem = document.getElementById('myFancyDiv');
 
var isInViewport = inViewport(elem); // returns `true` or `false` 
 
alert('myFancyDiv is ' + isInViewport ? 'visible' : 'not visible' + ' in the window');

We watch for your element to enters the viewport and call your callback when it does.

var inViewport = require('in-viewport');
var elem = document.getElementById('myFancyDiv');
 
inViewport(elem, visible);
 
function visible(elt) {
  // elt === elem 
  alert(elt.id + ' is visible in the window !');
}

The first callback argument is always the element that entered the viewport.

By default, we use the current window as the reference viewport. But you can also specify another element as a reference viewport.

var inViewport = require('in-viewport');
var customContainer = document.getElementById('myFancyContainer');
var elem = document.getElementById('myFancyDiv');
 
inViewport(elem, { container: customContainer }, visible);
 
function visible() {
  alert('myfancyDiv is visible in the `customContainer` !');
}

By default, when your element precisely enters the viewport, you get a callback.

But maybe you want to know when your element is soon-to-be-shown in the viewport?

Use the offset param for that!

var inViewport = require('in-viewport');
var elem = document.getElementById('myFancyDiv');
 
inViewport(elem, { offset: 300 }, visible);
 
function visible() {
  alert('myfancyDiv is visible in the `customContainer` !');
}

When your element is near 300px of the viewport, you get your callback / true result.

If you are creating elements dynamically, be sure to call inViewport when the element is in the DOM.

Otherwise it may fail on old browsers.

We check for newly visible elements on scroll or resize.

We use MutationObserver to listen for newly added DOM nodes that were previously registered with in-viewport.

MutationObserver is not compatible with old browsers.

That is why, if you need old browsers full compatibility, you should call in-viewport after inserting elements in the DOM.

  • Images, iframes, widgets lazyloader
  • infinite scroll
  • loading widgets only when needed

Be sure to be in standards-compliant mode.

Quirks mode is not supported since most browsers will report invalid values for window viewport.

Launch the dev server:

npm run dev

Browse to http://localhost:8080/__zuul.

Tests are written with mocha.

We provide a pre-built version of in-viewport in build/in-viewport.min.js.

But you can build your own:

npm run build

You get the build in build/in-viewport.min.js.

Please consider using browserify.

Copyright (c) 2013 Vincent Voyer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.