I'm building a simple Simon Says/Memory app and am trying to use jQuery to animate some divs where the user repeats the pattern back. The part I'm getting stuck on is lighting up the divs in sequence.
My initial thought was to use an array [1,4,2,3,1,4]
that matches up with the id's of specific divs
HTML
<div class="container">
<div id="1" class="red square"></div>
<div id="2" class="yellow square"></div>
<div id="3" class="blue square"></div>
<div id="4" class="green square"></div>
</div>
CSS
.square{
width: 200px;
height: 50px;
opacity: 0.2;
}
.brighten {
opacity: 1;
}
From here, I'd loop through the array and do something like:
for( var i = 0; i < arr.length; i++){
var element = $("#"+arr[i]);
element.addClass('brighten');
//pause for a half second
element.removeClass('brighten');
}
being a JS/jQuery novice I'm struggling because the divs are being brighten
and unbrighten
all at once.
I looked at using jQuery
animate()
but when I use the following code
for (var i = 0; i < arr.length; i++) {
$("#" + arr[i]).animate({
opacity: 1,
}, 500, function() {
// Animation complete.
$("#" + arr[i]).animate({
opacity: 0.2,
}, 500, function() {
// Animation complete.
});
});
}
all of the divs are being highlighted at once rather than in sequence.