All Projects → camwiegert → In View

camwiegert / In View

Licence: mit
Get notified when a DOM element enters or exits the viewport. 👀

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to In View

Mac Mouse Fix
Mac Mouse Fix - A simple way to make your mouse better.
Stars: ✭ 362 (-92.27%)
Mutual labels:  utility
Macroable
A trait to dynamically add methods to a class
Stars: ✭ 415 (-91.14%)
Mutual labels:  utility
Vue Mq
📱 💻 Define your breakpoints and build responsive design semantically and declaratively in a mobile-first way with Vue.
Stars: ✭ 474 (-89.88%)
Mutual labels:  viewport
Version Checker
Kubernetes utility for exposing image versions in use, compared to latest available upstream, as metrics.
Stars: ✭ 371 (-92.08%)
Mutual labels:  utility
React Socks
🎉 React library to render components only on specific viewports 🔥
Stars: ✭ 400 (-91.46%)
Mutual labels:  viewport
Ts Prune
Find unused exports in a typescript project. 🛀
Stars: ✭ 412 (-91.2%)
Mutual labels:  utility
React On Screen
Check if a react component in the viewport
Stars: ✭ 357 (-92.38%)
Mutual labels:  viewport
Fakeit
The Kotlin fake data generator library!
Stars: ✭ 482 (-89.71%)
Mutual labels:  utility
Lodash Php
Easy to use utility functions for everyday PHP projects. This is a port of the Lodash JS library to PHP
Stars: ✭ 412 (-91.2%)
Mutual labels:  utility
Jumpstate
Jumpstate is a simple and powerful state management utility for Redux.
Stars: ✭ 429 (-90.84%)
Mutual labels:  utility
Gdx Texture Packer Gui
A simple way to pack and manage texture atlases for LibGDX game framework.
Stars: ✭ 371 (-92.08%)
Mutual labels:  utility
Common.utility
Various helper class
Stars: ✭ 4,101 (-12.45%)
Mutual labels:  utility
Remarshal
Convert between CBOR, JSON, MessagePack, TOML, and YAML
Stars: ✭ 421 (-91.01%)
Mutual labels:  utility
Jql
A JSON Query Language CLI tool
Stars: ✭ 368 (-92.14%)
Mutual labels:  utility
K2tf
Kubernetes YAML to Terraform HCL converter
Stars: ✭ 477 (-89.82%)
Mutual labels:  utility
Vuex Rest Api
A utility to simplify the use of REST APIs with Vuex
Stars: ✭ 365 (-92.21%)
Mutual labels:  utility
Rm Protection
A safe alternative for "rm".
Stars: ✭ 416 (-91.12%)
Mutual labels:  utility
Motus
Animation library that mimics CSS keyframes when scrolling.
Stars: ✭ 502 (-89.28%)
Mutual labels:  viewport
Git Fresh
🍋 Keep your Git repo fresh.
Stars: ✭ 480 (-89.75%)
Mutual labels:  utility
Keepingyouawake
Prevents your Mac from going to sleep.
Stars: ✭ 4,343 (-7.28%)
Mutual labels:  utility

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+.

camwiegert.github.io/in-view

Build Status npm/in-view

in-view.js


Installation

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.

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 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);

inView.is(<element>)

Check if element is in the viewport.

inView.is(document.querySelector('.someSelector'));
// => true

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 least 100 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 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.

inView.threshold(0);
inView.threshold(0.5);
inView.threshold(1);

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

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

inView(<selector>).check()

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('.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 (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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].