I'm building an animation where I want a series of <div>
s to appear over time (changing their opacity from 0 to 1), in an order that I assigned to them. So far, I've got the first part working: the <div>
s appear on the screen one by one.
This is my HTML:
<div id="parent_container">
<div n="2">1</div>
<div n="1">2</div>
<div n="3">3</div>
<div n="4">4</div>
<div n="5">5</div>
<div n="7">6</div>
<div n="6">7</div>
<div n="8">8</div>
<div n="9">9</div>
</div>
(With some CSS for background color, opacity, etc.) And this is my function:
var delay = 0;
$('div').each(function(){
$(this).delay(delay).animate({
opacity:1,
},500);
delay += 500;
});
You can fiddle with my code here: http://jsfiddle.net/VS8tQ/250/
But now I want to sort those <div>
s as the animation is running, so that <div n="1">
(in this case '2') appears before <div n="2">
(in this case '1'). What I need seems to be very similar to a question that was asked here: How to sort DOM elements while selecting in jQuery? (the solution of which has been copied as a comment in the above fiddle). But I can't seem to get it to work with a different attribute than @id
, which is a problem for me (because I need that attribute for something else).
Any suggestions?