Lazy Animate
Use CSS animations on load or scroll.
Installation
-
Install package as dependency
yarn add lazyanimate# ornpm install lazyanimate -
Import functions in your script and create an instance
const lazyAnimate =
Basic Usage
Lazy animate an element by adding a class of lazyanimate
and a data-animate
attribute with the CSS keyframes animation name:
<!-- HTML -->
Add CSS animation keyframes and apply duration and timing function:
Note that you should not apply the animation name to your elements as lazyanimate will do this for you via the data-animate attribute.
/* CSS */ @}
Initialize lazyanimate in your JavaScript:
const lazyAnimate = lazyAnimate
data-animate attribute
The data-animate
attribute accepts either a string or a JavaScript object.
If a string is passed in, the element will have the CSS property animation-name
applied to it when your lazyAnimate JS triggers it.
Eg: data-animate="slide-in"
If a JS object is passed in, you can apply any css property prefixed with animation-
.
data-animate="{ name: '', delay: 0, direction: '', duration: 0, fillMode: '',iterationCount: 1, playState: '', timingFunction: '' }"
You can also change the animation name using a media query string.
You should have one key with a value of true
which will be the fallback animation.
data-animate="{ name: { 'slide-in': true, 'slide-in-tablet': '(min-width:768px)' } }"
API
lazyAnimateAllOnLoad
Loads all lazyanimate animations on load or instantly if already loaded.
const lazyAnimate = lazyAnimate
lazyAnimateElement
Applies a CSS animation to an element based on it's data-animate.
const el = document const lazyAnimate = lazyAnimate
lazyAnimateOnScroll
Applies lazy animate to all elements when intersection observer fires.
const scrollContainer = documentconst thresholdPercent = 08 const lazyAnimate = lazyAnimate