jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of

02.06.2013 Views

3. Save this as slide.html. Next create the following stylesheet: #image { position:relative; float:left; } #image img { margin-bottom:-5px; } #image div { display:none; width:100%; padding:10px 0; position:absolute; left:0; bottom:0; top:auto !important; text-align:center; font-style:italic; background-color:#000; color:#fff; } 4. Save this file as slide.css. [ 169 ] Chapter 6 5. When we view the page we should find that the caption is displayed as soon as we move the mouse over the image, and then removed when we move the mouse off it: In the previous screenshot we see the caption sliding out from the left edge of the container. What just happened? The image and caption are held in a container so that the caption can be positioned accurately. We use jQuery's hover() method, which allows us to attach event handlers for both the mouseover and mouseout events, to show the caption by sliding it in, or hide it by sliding it out.

Extended Animations with jQuery UI We don't need any additional configuration in this simple example, but we do need to manage the queue effectively to stop a build up of animations if the mouse pointer is moved on and off the image repeatedly, which we handle with the stop() method. The scale effect The scale effect is very similar to the size effect that we looked at earlier, and as we saw, several effects actually require this effect as a dependency. The main difference between this effect and the size effect is that with scale, we can only specify a percentage that the target element should be scaled to, not supply exact pixel sizes. Configuration options The scale effect has more configuration options than any other effect added by jQuery UI. These options are listed in the following table: Option Default Usage direction "both" Sets which axis the element is scaled along. from none Sets the starting dimensions of the element. origin ['middle', 'center'] Sets the vanishing point of the element if it is being hidden, or the point from which it grows if it is being shown. percent 0 Sets the percentage by which the element will grow or shrink. scale "both" This option sets whether the whole box of the element (including border and padding CSS values) is scaled, just the content, or as in the default, both. Time for action – scaling an element It's common practice on an image-heavy site to show a set of thumbnail images which link to a full-sized image which is displayed when the image is clicked, either inline in a modal pop up, or in a separate window. In this example we'll create a thumbnail image that scales to a full-sized version when clicked. 1. Add the following few elements to the of the template file: [ 170 ]

3. Save this as slide.html. Next create the following stylesheet:<br />

#image { position:relative; float:left; }<br />

#image img { margin-bottom:-5px; }<br />

#image div {<br />

display:none; width:100%; padding:10px 0; position:absolute;<br />

left:0; bottom:0; top:auto !important; text-align:center;<br />

font-style:italic; background-color:#000; color:#fff;<br />

}<br />

4. Save this file as slide.css.<br />

[ 169 ]<br />

Chapter 6<br />

5. When we view the page we should find that the caption is displayed as soon as<br />

we move the mouse over the image, and then removed when we move the mouse<br />

<strong>of</strong>f it:<br />

In the previous screenshot we see the caption sliding out from the left edge <strong>of</strong> the container.<br />

What just happened?<br />

The image and caption are held in a container so that the caption can be positioned<br />

accurately. We use <strong>jQuery</strong>'s hover() method, which allows us to attach event handlers for<br />

both the mouseover and mouseout events, to show the caption by sliding it in, or hide it by<br />

sliding it out.

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

Saved successfully!

Ooh no, something went wrong!