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

[ 149 ] Chapter 6 In the previous screenshot, we see the text fields being shaken when they are left empty and the is clicked. What just happened? When the is clicked we simply check to see if each has a value and if not, we apply a red border and then call the effect() method specifying shake as the effect. We use a configuration object to reduce the distance the element moves, as well as specifying a relatively short duration. The size effect The size effect is used to resize an element, making it grow or shrink depending on its configuration. Unlike most of the other effects, the size effect must be configured for it to be used successfully. The size effect is also one of the only effects that has the base core file as well as another effect as dependencies. Most components rely only on the core file. As we downloaded the entire effect suite from the jQuery UI download builder, we don't need to worry about including the additional effect. It's already in the single file that the download builder created when we downloaded it at the start of the chapter. Configuration options The size effect gives us four configurable options, which are listed as follows: Option Default Usage from none Sets the size of the target element at the beginning of the animation. This option accepts an object with height and width keys which are used to set the starting size of the target element. This option is not mandatory. to none Sets the size of the target element at the end of the animation. This option accepts an object with height and width keys which are used to set the ending size of the target element. This option must be supplied. origin ['middle ', 'center'] Sets the vanishing point for hiding animations, or the point from which it grows when used with show logic. 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.

Download from Wow! eBook Extended Animations with jQuery UI Time for action – resizing elements A popular use of growing and shrinking elements is the Fisheye menu, where elements grow when the mouse pointer hovers over them, and shrink back down when the pointer moves off them. This effect is also used by the icons on the dock in Apple's OSX. Using the size effect, we can implement our own basic Fisheye menu with just a few lines of code. 1. Add the following markup to the of the template file: 2. Add the following JavaScript to the third element at the bottom of the : $(".icon", "#dock").hover(function() { $(this).stop().animate({ top: -31 }).find("img").stop().effect("size", { to: { width: 64, height: 64 } }); }, function() { $(this).stop().animate({ top: -15 }).find("img").stop().effect("size", { to: { width: 48, height: 48 } }); }); 3. Save this file as size.html. We also need some styling. In a new file add the following code: #dock { width:380px; height:90px; position:fixed; bottom:0; [ 150 ]

Download from Wow! eBook <br />

Extended <strong>Animation</strong>s with <strong>jQuery</strong> UI<br />

Time for action – resizing elements<br />

A popular use <strong>of</strong> growing and shrinking elements is the Fisheye menu, where elements grow<br />

when the mouse pointer hovers over them, and shrink back down when the pointer moves<br />

<strong>of</strong>f them. This effect is also used by the icons on the dock in Apple's OSX.<br />

Using the size effect, we can implement our own basic Fisheye menu with just a few lines<br />

<strong>of</strong> code.<br />

1. Add the following markup to the <strong>of</strong> the template file:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

2. Add the following JavaScript to the third element at the bottom <strong>of</strong> the<br />

:<br />

$(".icon", "#dock").hover(function() {<br />

$(this).stop().animate({<br />

top: -31<br />

}).find("img").stop().effect("size", {<br />

to: { width: 64, height: 64 }<br />

});<br />

}, function() {<br />

$(this).stop().animate({<br />

top: -15<br />

}).find("img").stop().effect("size", {<br />

to: { width: 48, height: 48 }<br />

});<br />

});<br />

3. Save this file as size.html. We also need some styling. In a new file add the<br />

following code:<br />

#dock {<br />

width:380px; height:90px; position:fixed; bottom:0;<br />

[ 150 ]

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

Saved successfully!

Ooh no, something went wrong!