I’ve been rebuilding a WordPress site that was originally set up using a popular ThemeForest theme and Visual Composer. The theme made it really easy to add animation effects that occur on scroll (as specific elements come into view), but extracting the code for animations wasn’t super easy (it’s part of a 600kb javascript included by theme and mixed with inline styles from Visual Composer).
However, after testing a few different libraries, I found Scroll Reveal worked best as a replacement (3k minified & gzipped).
Basic Set Up
If you want to use Scroll Reveal with default settings, just enqueue it in your theme:
<?php
wp_enqueue_script(
'prefix-scrollreveal',
get_template_directory_uri() . '/js/scrollreveal.min.js',
array(),
'1.0.0',
true
);
And call then call it on the elements you’d like to animate on scroll.
Customize Animations
The theme I’ve been rebuilding needed unique animations on multiple elements (custom zooms, durations, scaling, delays, etc.), which I didn’t want to set up as individual Scroll Reveal calls. Instead, I decided to use data attributes which could be applied directly in the markup.
Here’s an example:
<div class="product" data-animation="true" data-duration="1000" data-scale="0.9" data-opacity=".8" data-distance="40px">
<img src="<?php echo esc_url( get_template_directory_uri() ); ?>/images/product.png">
</div>
Any element that has the data attribute “data-animation”, will be animated by Scroll Reveal. The other data attributes “scale”, “opacity”, and “distance” override Scroll Reveal defaults.
Here’s how you can initialize Scroll Reveal to use data attributes:
https://gist.github.com/devinsays/aad496470af20910f4009380d94a1bb4#file-theme-js
Scroll Reveal used to support data attributes by default, but no longer does. So hopefully this helps simplify things if you’re also needing to set up a site with with multiple unique Scroll Reveal animations.
