My goal is to add animations that trigger as the user scrolls, but I want them to be staggered so they occur one after the other in quick succession.
I attempted using a function that adds a class to trigger CSS animations and then used setTimeout to delay the next animation slightly. However, instead of each animation being delayed, they all fade in simultaneously...
JSFiddle
UPDATE: It seems like I need to handle this in JavaScript since I have multiple types of animations on one page and need to introduce a few milliseconds delay between adding the classes.
/**
* Check if Animation is currently in view
*/
function anim_in_view() {
var window_height = $(window).height();
var window_top_position = $(window).scrollTop();
var window_bottom_position = (window_top_position + window_height);
var $animations = $('body .animate');
if ($animations.length) {
$.each($animations, function() {
var $elm = $(this);
var element_height = $elm.outerHeight();
var element_top_position = $elm.offset().top + 50;
var element_bottom_position = (element_top_position + element_height);
setTimeout(function() {
if ((element_bottom_position >= window_top_position) &&
(element_top_position <= window_bottom_position)) {
// $elm.delay( 2000 ).addClass( 'visible' );
$elm.addClass('visible');
}
}, 1000);
});
}
}
$(window).on('load scroll resize', anim_in_view);
.flex {
margin-top: 1000px;
margin-bottom: 500px;
display: -webkit-flex;
display: flex;
}
.flex > div {
width: 33.33%;
height: 200px;
}
.red {
background: #f00;
}
.green {
background: #0f0;
}
.blue {
background: #00f;
}
.animate-opacity {
opacity: 0;
-webkit-transition: opacity 1s ease-in-out;
transition: opacity 2s ease-in-out;
}
.animate-opacity.visible {
opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="fadeIns" class="flex">
<div class="animate animate-opacity red"></div>
<div class="animate animate-opacity green"></div>
<div class="animate animate-opacity blue"></div>
</div>