Wednesday, February 1, 2012

Lesson 3: Callbacks

Last time we spoke of stacking animations, that was fun. But what if you want to start doing something with another element when some animation is finished? Asynchronous programming comes in handy not only when calling server side. Let's see how callbacks work for jQuery animations.

Just a few lines of code and we have a MIDI-keyboardish thingie full of colored square keys to click: Yes, they are clickable!

Pretty much done already, could employ a couple of swell modern features though. What kind of a midi-keyboard cannot do anything on it's own? Playing sequences after just one touch is a must-have feature, time to code it! Pressing one key will start off a bunch of pitches, as if we pressed some of them sequentially. When launching an animation on a key, we need to know which one goes next. We'll link elements using their data field. Have a look at jQuery .data, nice one. Now that for each element we know who's next, we can enhance the animation function by adding a callback. Callback practice is over, enjoy the result of all this hard work :)

Source code links:

Move on to the next lesson!

1 comment :

  1. Great Post ! jQuery Tutorial & jQuery Interview Questions with simple and easy examples,Covering JQuery Introduction, Selectors, Hide / Show Effect, Fade Effect, Slide Effect, Animation, Event Handling, Keyboard Event, Mouse Event, Form Events, Traversing, Ajax. HTML5 Tutorial & CSS3 Tutorial.

    ReplyDelete