This plugin is created to add simple inview animations to your website. It uses IntersectionObserver and simple CSS to create elegant inview animations.
Install package via NPM
npm i inview-observer.js --save
<div class="js-inview-element" data-inview-delay="500"></div>
.js-inview-element {
transition .5s cubic-bezier(.83, 0, .17, 1);
transition-property: opacity, transform;
opacity: 0;
}
.js-inview-element.is-inview {
opacity: 1;
transform: translate(0, 0)
}
import InviewObserver from 'inview-observer.js';
document.addEventListener('DOMContentLoaded', () => {
const inviewObserver = InviewObserver();
inviewObserver.init();
});
You can overwrite the default options:
inviewObserver.init({
element: '.js-inview-element',
observerOptions: {
rootMargin: '100px 0px',
threshold: 0.75
},
data: {
delay: 'data-inview-delay'
},
classes: {
inview: 'is-inview'
},
});
Option | Type | Default | Description |
---|---|---|---|
element |
String | .js-inview-element |
Element that needs to be animated when inview. |
observerOptions |
Object | {rootMargin: '0px 0px', threshold: 0.5} |
IntersectionObserver options |
data |
Object | {delay: 'data-inview-delay'} |
Data attribute containing the delay of the inview class |
classes |
Object | {inview: 'is-inview'} |
Class that is added when element is inview |
We have added an event that you can dispatch if you want to reinit the IntersectionObserver. Especially handy if you have async elements.
const reInitInview = new Event('inview-reinit');
window.dispatchEvent(reInitInview);