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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

prox.mousemove(function(ev) {<br />

goAnim(ev);<br />

});<br />

});<br />

prox.mouseleave(function() {<br />

scroller.stop();<br />

prox.unbind("mousemove");<br />

});<br />

[ 215 ]<br />

Chapter 8<br />

What just happened?<br />

First we set a mouseeenter event handler so that we can detect when the pointer initially<br />

enters the proximity container. When this occurs we change the message text so that it<br />

shows what to do with the mouse pointer and then fade out the message slowly after a<br />

delay <strong>of</strong> one second.<br />

We then call our goAnim() function to start the animation. At this point, we set a<br />

mousemove event so that we can increase or decrease the speed <strong>of</strong> the animation as the<br />

pointer moves within the proximity container. Each time the pointer moves, we call the<br />

goAnim() function once more. Each time this function is called we pass in the event object.<br />

We also set a mouseleave event handler on the proximity container so that we can detect<br />

when the pointer leaves this element altogether. When this occurs we stop the currently<br />

running animation and unbind the mousemove event handler.<br />

At this point we should have a fully working proximity slider. Earlier we discussed how the<br />

proximity effect is only useful to mouse users, so let's add a keyboard event handler to our<br />

script that will let keyboard users navigate the scroller as well.<br />

Time for action – adding keyboard events<br />

The following code enables keyboard driven animations:<br />

$(document).keydown(function(e) {<br />

if (e.keyCode === 37 || e.keyCode === 39) {<br />

message.fadeOut("slow");<br />

if (!scroller.is(":animated")) {<br />

scroller.stop().animate({<br />

left: (e.keyCode === 37) ? 0 : -(scroller.width() -

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

Saved successfully!

Ooh no, something went wrong!