Skip to content

Instantly share code, notes, and snippets.

@willbroderick
Created June 17, 2014 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willbroderick/001ea9422f9743caca98 to your computer and use it in GitHub Desktop.
Save willbroderick/001ea9422f9743caca98 to your computer and use it in GitHub Desktop.
Stagger events across a collection of elements
/*
Stagger an event across a collection of elements. Useful for making the pretties.
Example - for loading in ajax results with a class-based css3 transition, where results are initially hidden with hide():
$('#ajax-results').on('loaded', function(){
$(this).children('.result').addClass('pre-transition').slideDown(250).staggerEvent(function(el){
$(el).removeClass('pre-transition');
}, 150, 250);
});
*/
$.fn.staggerEvent = function(ev, delay, initialDelay){
var ev = ev, delay = delay, $els = $(this);
if(typeof initialDelay === 'undefined') initialDelay = 0;
setTimeout(function(){
$els.each(function(i){
var $this = $(this);
setTimeout(function(){
if(typeof ev === 'function') {
ev($this);
} else {
$this.trigger(ev);
}
}, delay * i);
});
}, initialDelay);
return $(this);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment