Noob in jQuery.
I’m trying to cycle through an image gallery with the goal of having the images fade in and fade out.
Here is my JSFiddle
By clicking on #gallery each image fadeTo(arg1) and then fadeTo(args2).
But I want them to go one by one.
Perhaps a delay should be added to the chain?
Any help would be greatly appreciated.
jQuery animations (as most things on javascript) are asynchronous, which means that the function calls return before the action has finished. In a sense, you can think of each iteration of your
$('img').eachloop as following:thisbound to oneimgelementfadeon$(this)Then, after all your iterations have ended the browser will start to make the animations happen on the screen, all at the same time (since all of them were scheduled).
To create a sequential behavior, you can give the
fadefunction a callback:Then, instead of scheduling all animations at once, what happens is something like this:
imgelements are stored in theremainingImagesarrayprocessNextfunction is called, which removes the first image from the arrayfadeon the$(image), providing a function (that happens to be itself) that should be called after the animation is finishedUpdated jsFiddle