javascript - Animating long sequences in jQuery -


i have make long animation jquery, full of fadeouts,fadeins,slideins,...

the problem having code looks ugly , full of callback.
also, if want stop animation time like: slideout->wait 5 seconds->slidein have use delay , not sure if best practice.

example:

/* slides */ var slide1 = $('div#slide1'),     slide2 = $('div#slide2'),     slide3 = $('div#slide3');  $(document).ready(function(){     slide1.fadein(function(){         slide2.fadein(function(){             slide3.fadein().delay(3000).fadeout(function(){                 slide2.fadeout(function(){                     slide1.fadeout();                 });             });         });     }); }); 

jsfiddle: http://jsfiddle.net/zpvrd/6/

question: there other way of building animations in jquery, possibly great plugin me solve problem?

thanks!

here's plugin looking :) exact same thing, more flexible existing code http://jsfiddle.net/zpvrd/11/

(function($){     $.fn.fadeinout = function(middledelay) {         middledelay = middledelay || 0;          var index = 0,             direction = 1, // 1: fading in; -1: fading out                     me = this,             size = me.size();         function nextanimation() {             // before first element, we're done             if (index === -1 )  { return; }              var currentel = $(me.get(index)),                 goingforward = direction === 1,                 islastelement = index === (size - 1);              // change direction next animation, don't update index             // since next frame fade same element out             if (islastelement && goingforward) {                 direction = -1;             } else {                 index += direction;             }              // @ last element, before starting fade out, add delay              if ( islastelement && !goingforward) {                 currentel.delay(middledelay);             }             if (goingforward) {                 currentel.fadein(nextanimation);             } else {                 currentel.fadeout(nextanimation);                                 }         }         nextanimation();         return this;     }         })(jquery); 

and call like

$('div.slidewrapper>div.slide').fadeinout(3000); 

this process of traversing , down list of jquery elements waiting each animation finish abstracted used other things besides fadein , fadeout. i'll leave try out if feel adventurous.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -