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.

Fading <strong>Animation</strong>s<br />

What just happened?<br />

The first thing we did was cache a reference to the located inside our element.<br />

We'll be referencing it several times so it is more efficient to only select it from the DOM a<br />

single time. For performance reasons, it is generally best to minimize the number <strong>of</strong> DOM<br />

operations that are carried out.<br />

We then remove the purecss class from the element so that our standard CSS hover states<br />

are no longer effective. We use JavaScript to do this so the basic hovers still work if JavaScript<br />

is disabled in the browser.<br />

Next we insert a new element into each <strong>of</strong> the anchors. We'll perform the<br />

animation on each <strong>of</strong> these elements so that anchors themselves remain clickable<br />

and functional. As the elements are styled to fit exactly within their parent <br />

elements, we also add the text from the anchor into the .<br />

Finally, we use <strong>jQuery</strong>'s hover() method to attach mouseover and mouseout event<br />

handlers to the elements. This method accepts two functions; the first is executed on<br />

mouseover, the second on mouseout.<br />

In the first function, we select the inside the that was hovered on and call<br />

the fadeIn() method specifying the slow string. In the second function, we simply hide<br />

the again. When we run the page in a browser, we find that the hover states are<br />

activated when we hover over the list items. It's a great effect, and one I use frequently<br />

when building clients' sites. In this example, we have a potential issue in that the link text<br />

is duplicated in the elements that are added to each link. It would be trivial to hide<br />

the link text when the is made visible, which we could do using a callback function<br />

supplied as an argument to the fadeIn() method.<br />

Pop quiz – using fadeIn<br />

1. Which strings can be passed into the method as the first argument?<br />

a. The strings short or long, which refer to the duration <strong>of</strong> the animation<br />

b. The strings low or high, which refer to the opacity that the element<br />

is faded to<br />

c. The strings slow or fast, which refer to the duration <strong>of</strong> the animation<br />

d A hexadecimal string specifying the background-color <strong>of</strong> the element<br />

[ 26 ]

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

Saved successfully!

Ooh no, something went wrong!