02.06.2013 Views

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Other Popular <strong>Animation</strong>s<br />

prox.width())<br />

}, 6000, "linear");<br />

}<br />

}<br />

}).keyup(function() {<br />

scroller.stop();<br />

});<br />

What just happened?<br />

We attach the keydown event handler to the document object so that the visitor doesn't<br />

have to focus the proximity container somehow. Within the anonymous function, we first<br />

check whether the left or right arrow keys were pressed.<br />

The key code 37 refers to the left arrow key and the code 39 refers to the right arrow key.<br />

The keyCode property, normalized by <strong>jQuery</strong> so that it is accessible to all browsers, will<br />

contain the code for whichever key was pressed, but we only want to react to either <strong>of</strong> the<br />

specified keys being pressed.<br />

When either <strong>of</strong> these keys are pressed, we first fade out the message and then check that<br />

the scroller is not already being animated using <strong>jQuery</strong>'s is() method in conjunction with<br />

the :animated filter.<br />

As long as the scroller element is not already being animated (denoted by the ! at the start<br />

<strong>of</strong> the condition), we then animate it. We check the keyCode once again with a ternary so<br />

that we can move the scroller in the correct direction depending on which key is pressed.<br />

Finally we add a keyup event handler that stops the scroller animation once the key is<br />

released. This improves the interactivity <strong>of</strong> animation as it allows the visitor to intuitively<br />

stop the scroller whenever they wish.<br />

Have a go hero – extending proximity animations<br />

The obvious way to extend our example would be to trigger animations on the vertical axis<br />

as well. We could have a grid <strong>of</strong> images instead <strong>of</strong> a single row and animate the grid up and<br />

down as well as left and right.<br />

One thing to do to extend the example would be to add additional keyboard functionality.<br />

Check for additional keys such as the home and end keys for example, which could navigate<br />

to the start or end <strong>of</strong> the scroller element accordingly.<br />

[ 216 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!