Top Related Projects
Animate elements as they scroll into view.
Animate on scroll library
Reveal CSS animation as you scroll down a page
Waypoints is a library that makes it easy to execute a function whenever you scroll to an element.
Simple & lightweight (<4kb gzipped) vanilla JavaScript library to create smooth & beautiful animations when you scroll.
LazyLoad is a lightweight, flexible script that speeds up your website by deferring the loading of your below-the-fold images, backgrounds, videos, iframes and scripts to when they will enter the viewport. Written in plain "vanilla" JavaScript, it leverages IntersectionObserver, supports responsive images and enables native lazy loading.
Quick Overview
in-view is a lightweight JavaScript library for detecting when elements enter or exit the viewport. It provides a simple API to track and respond to elements' visibility on the page, making it useful for implementing lazy loading, infinite scrolling, or triggering animations based on scroll position.
Pros
- Lightweight and dependency-free
- Easy to use with a straightforward API
- Supports both entering and exiting viewport events
- Allows for custom offset thresholds
Cons
- Limited features compared to more comprehensive intersection observer libraries
- May not perform as well as native IntersectionObserver API for complex scenarios
- Lacks advanced options for controlling when callbacks are triggered
- Not actively maintained (last update was in 2018)
Code Examples
- Basic usage:
inView('.selector')
.on('enter', el => {
console.log(el, 'entered the viewport');
})
.on('exit', el => {
console.log(el, 'exited the viewport');
});
- Setting a custom offset:
inView.offset(100);
inView('.selector')
.on('enter', el => {
console.log(el, 'entered 100px before the viewport');
});
- Checking if an element is in view:
const element = document.querySelector('.selector');
if (inView.is(element)) {
console.log('Element is in view');
}
Getting Started
- Install the library using npm:
npm install in-view
- Import and use in your JavaScript file:
import inView from 'in-view';
inView('.my-element')
.on('enter', el => {
el.classList.add('visible');
})
.on('exit', el => {
el.classList.remove('visible');
});
- Alternatively, include the script directly in your HTML:
<script src="https://unpkg.com/in-view@0.6.1/dist/in-view.min.js"></script>
<script>
inView('.my-element').on('enter', function(el) {
el.classList.add('visible');
});
</script>
Competitor Comparisons
Animate elements as they scroll into view.
Pros of ScrollReveal
- More feature-rich with advanced animation options
- Supports both JavaScript and CSS-based animations
- Extensive documentation and examples
Cons of ScrollReveal
- Larger file size and potentially higher performance impact
- Steeper learning curve due to more configuration options
Code Comparison
ScrollReveal:
ScrollReveal().reveal('.element', {
delay: 200,
distance: '50px',
origin: 'bottom',
easing: 'cubic-bezier(0.5, 0, 0, 1)'
});
in-view:
inView('.element')
.on('enter', el => el.classList.add('visible'))
.on('exit', el => el.classList.remove('visible'));
Summary
ScrollReveal offers more advanced animation capabilities and customization options, making it suitable for complex scroll-based animations. However, this comes at the cost of a larger file size and potentially more complex setup.
in-view, on the other hand, provides a simpler API focused on detecting when elements enter or exit the viewport. It's lightweight and easy to use but lacks built-in animation features.
Choose ScrollReveal for projects requiring intricate scroll-based animations, and in-view for simpler viewport detection needs or when performance is a primary concern.
Animate on scroll library
Pros of AOS
- More animation options and customization
- Supports CSS3 transitions and keyframe animations
- Includes a built-in library of pre-defined animations
Cons of AOS
- Larger file size and potentially higher performance impact
- More complex setup and configuration
- May require additional CSS knowledge for custom animations
Code Comparison
in-view:
inView('.selector')
.on('enter', el => {
el.classList.add('in-view');
});
AOS:
<div data-aos="fade-up" data-aos-duration="1000">
Animated element
</div>
AOS.init();
in-view focuses on detecting when elements enter or leave the viewport, providing a simple API for adding classes or triggering callbacks. It's lightweight and easy to use but offers limited animation capabilities.
AOS, on the other hand, provides a more comprehensive animation solution with pre-defined effects and greater customization options. It requires less JavaScript knowledge but more markup and potentially more CSS skills for advanced usage.
Choose in-view for simple viewport detection and lightweight implementations. Opt for AOS when you need a wider range of animation options and don't mind the additional overhead.
Reveal CSS animation as you scroll down a page
Pros of WOW
- Offers a wide range of animation effects out-of-the-box
- Provides easy customization options for animations
- Includes CSS-only animations, reducing JavaScript overhead
Cons of WOW
- Larger file size due to the extensive animation library
- May require more setup and configuration for complex use cases
- Less focused on viewport detection compared to in-view
Code Comparison
WOW:
new WOW().init();
<div class="wow fadeInUp" data-wow-duration="2s" data-wow-delay="1s">
Animated content
</div>
in-view:
inView('.selector')
.on('enter', el => {
el.classList.add('animated');
});
Key Differences
- WOW focuses on providing pre-built animations, while in-view emphasizes viewport detection
- in-view offers more granular control over when elements are considered in view
- WOW requires less JavaScript for basic animations but may have a higher initial load time
- in-view provides a more flexible API for custom animations and interactions
Both libraries serve different purposes: WOW is ideal for quick, pre-defined animations, while in-view is better suited for custom viewport-based interactions and more complex scenarios.
Waypoints is a library that makes it easy to execute a function whenever you scroll to an element.
Pros of Waypoints
- More feature-rich with advanced options for callbacks and offsets
- Supports horizontal scrolling in addition to vertical
- Better browser compatibility, including older versions
Cons of Waypoints
- Larger file size and potentially higher performance overhead
- More complex API, which may be overkill for simple use cases
- Less frequent updates and maintenance
Code Comparison
In-view:
inView('.selector')
.on('enter', el => {
el.classList.add('in-view');
});
Waypoints:
var waypoint = new Waypoint({
element: document.querySelector('.selector'),
handler: function(direction) {
this.element.classList.add('in-view');
}
});
Both libraries aim to detect when elements enter the viewport, but Waypoints offers more granular control at the cost of a slightly more verbose API. In-view provides a simpler, more streamlined approach for basic use cases, while Waypoints is better suited for complex scrolling interactions and older browser support.
Simple & lightweight (<4kb gzipped) vanilla JavaScript library to create smooth & beautiful animations when you scroll.
Pros of lax.js
- More advanced animation capabilities, including parallax effects and complex scrolling animations
- Offers a wider range of pre-built effects and presets
- Provides more granular control over animation properties and timing
Cons of lax.js
- Steeper learning curve due to more complex API and configuration options
- Larger file size and potentially higher performance overhead for complex animations
- May be overkill for simple scroll-based visibility detection
Code Comparison
in-view:
inView('.selector')
.on('enter', el => {
el.classList.add('visible');
})
.on('exit', el => {
el.classList.remove('visible');
});
lax.js:
window.onload = function() {
lax.init();
lax.addDriver('scrollY', function() {
return window.scrollY;
});
lax.addElements('.selector', {
scrollY: {
opacity: [
['elInY', 'elCenterY', 'elOutY'],
[0, 1, 0],
]
}
});
}
Both libraries offer scroll-based functionality, but in-view focuses on simple visibility detection, while lax.js provides more advanced animation capabilities. in-view is easier to use for basic tasks, while lax.js offers greater flexibility and control for complex animations at the cost of increased complexity.
LazyLoad is a lightweight, flexible script that speeds up your website by deferring the loading of your below-the-fold images, backgrounds, videos, iframes and scripts to when they will enter the viewport. Written in plain "vanilla" JavaScript, it leverages IntersectionObserver, supports responsive images and enables native lazy loading.
Pros of vanilla-lazyload
- More comprehensive lazy loading solution, supporting images, videos, and iframes
- Offers advanced features like automatic retry on error and callback functions
- Actively maintained with regular updates and improvements
Cons of vanilla-lazyload
- Larger file size and potentially more complex setup compared to in-view
- May have a steeper learning curve for basic use cases
Code comparison
in-view:
inView('.selector')
.on('enter', el => {
el.classList.add('in-view');
});
vanilla-lazyload:
const lazyLoadInstance = new LazyLoad({
elements_selector: ".lazy",
callback_enter: (el) => {
el.classList.add('loaded');
}
});
Key differences
- in-view focuses on detecting when elements enter or leave the viewport
- vanilla-lazyload specializes in lazy loading various types of content
- in-view has a simpler API for basic viewport detection
- vanilla-lazyload offers more options and features for lazy loading
Use cases
- Choose in-view for simple viewport detection and lightweight implementation
- Opt for vanilla-lazyload when you need comprehensive lazy loading functionality
Both libraries are useful for improving page performance, but they cater to different specific needs within the realm of viewport-based operations.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
in-view.js :eyes:
Get notified when a DOM element enters or exits the viewport. A small (~1.9kb gzipped), dependency-free, javascript utility for IE9+.
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
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.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 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.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 (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
Top Related Projects
Animate elements as they scroll into view.
Animate on scroll library
Reveal CSS animation as you scroll down a page
Waypoints is a library that makes it easy to execute a function whenever you scroll to an element.
Simple & lightweight (<4kb gzipped) vanilla JavaScript library to create smooth & beautiful animations when you scroll.
LazyLoad is a lightweight, flexible script that speeds up your website by deferring the loading of your below-the-fold images, backgrounds, videos, iframes and scripts to when they will enter the viewport. Written in plain "vanilla" JavaScript, it leverages IntersectionObserver, supports responsive images and enables native lazy loading.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot