Created
June 17, 2014 10:19
-
-
Save willbroderick/001ea9422f9743caca98 to your computer and use it in GitHub Desktop.
Stagger events across a collection of elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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