I'm still learning jQuery, and the biggest issue I'm having is how to take several smaller sections of code and combine them into a much more efficient script.
For example, I have a gallery of 7 items, and every item fades in, but with a different delay. (So image number 1 fades in after 1 second, image number 2 after 1.5, image number 3 after half a second etc) so that they do not all fade in at the same time. See the second section here.
The problem is, my code ends up quite long as I'm repeating the same code but with a different delay and targeting a different div. I need advice on how to make this more efficient. E.g:
$( ".gal:first-child" ).delay(800).animate({
opacity:1
}, 1800, function() {
});
$( ".gal:nth-child(2)" ).delay(2000).animate({
opacity:1
}, 1800, function() {
});
$( ".gal:nth-child(3)" ).delay(800).animate({
opacity:1
}, 1800, function() {
});
$( ".gal:nth-child(4)" ).delay(1500).animate({
opacity:1
}, 1800, function() {
});
$( ".gal:nth-child(5)" ).delay(800).animate({
opacity:1
}, 1800, function() {
});
$( ".gal:nth-child(6)" ).delay(1900).animate({
opacity:1
}, 1800, function() {
});
$( ".gal:last-child" ).delay(1500).animate({
opacity:1
}, 1800, function() {
});
- How would you make this more efficient?
- Would randomising the delay between two set values help?
- How would you ensure all the divs in the gallery get selected? (Randomise nth-child 1-7 maybe?)