{"id":2003,"date":"2009-10-09T21:03:43","date_gmt":"2009-10-10T01:03:43","guid":{"rendered":"http:\/\/www.andysowards.com\/blog\/?p=2003"},"modified":"2009-10-22T19:04:49","modified_gmt":"2009-10-22T23:04:49","slug":"how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial","status":"publish","type":"post","link":"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/","title":{"rendered":"How to Add Flair to your Actions with jQuery &#8211; Part 2 &#8211; Javascript Tutorial"},"content":{"rendered":"<p>Welcome back to part 2 of Adding Flair to your Actions with jQuery! If you missed <a href=\"https:\/\/www.andysowards.com\/blog\/tutorials\/how-to-add-flair-to-your-actions-with-jquery-part-1-javascript-tutorial\/\">Part 1<\/a>, go ahead and read that first, or just move ahead!<\/p>\n<h1>Basic hide and show <\/h1>\n<p>The basic <i>.hide() <\/i> and <i>.show() <\/i> methods, without any parameters, can be thought of as smart shorthand methods for <i>.css(&#8216;display&#8217;,&#8217;string&#8217;) <\/i>, where <i>&#8216;string&#8217; <\/i> is the appropriate display value. The effect, as might be expected, is that the matched set of elements will be immediately hidden or shown, with no animation. <\/p>\n<p>The <i>.hide() <\/i> method sets the <b>inline style attribute <\/b> of the matched set of elements to <i>display:none <\/i>. The smart part here is that it remembers the value of the <i>display <\/i> property&#8212;typically <i>block <\/i> or <i>inline <\/i>&#8212;before it was changed to <i>none <\/i>. Conversely, the <i>.show() <\/i> method restores the matched set of elements to whatever visible display property they had before <i>display:none <\/i> was applied. <\/p>\n<p style=\"margin-left: 20px; margin-right: 20px;\"> <em>For more information about the display property and how its values are visually represented in a web page, visit the Mozilla Developer Center at https:\/\/developer.mozilla.org\/en\/CSS\/display\/ and view examples at <a href=\"https:\/\/developer.mozilla.org\/samples\/cssref\/display.html\" target=\"_blank\">https:\/\/developer.mozilla.org\/samples\/cssref\/display.html<\/a>. <\/em> <\/p>\n<p>This feature of <i>.show() <\/i> and <i>.hide() <\/i> is especially helpful when hiding elements whose default display property is overridden in a stylesheet. For example, the   element has the property <i>display:block <\/i> by default, but we might want to change it to <i>display:inline <\/i> for a horizontal menu. Fortunately, using the <i>.show() <\/i> method on a hidden element such as one of these   tags would not merely reset it to its default <i>display:block <\/i>, because that would put the   on its own line. Instead, the element is restored to its previous <i>display:inline <\/i> state, thus preserving the horizontal design. <\/p>\n<p>A quick demonstration of these two methods can be set up by adding a second paragraph and a &#8220;read more&#8221; link after the first paragraph in the example HTML: <\/p>\n<p> <\/p>\n<pre style=\"margin-left: 20px;\"> <div id=\"switcher\"> <div class=\"label\">Text Size <\/div> <br\/> <button id=\"switcher-default\">Default <\/button> <br\/> <button id=\"switcher-large\">Bigger <\/button> <br\/> <button id=\"switcher-small\">Smaller <\/button> <br\/> <br\/> <\/div> <\/pre>\n<div class=\"speech\"> <\/p>\n<p>Fourscore and seven years ago our fathers brought forth <br \/>     on this continent a new nation, conceived in liberty, <br \/>     and dedicated to the proposition that all men are created <br \/>     equal. <\/p>\n<p> <\/div>\n<p>  <\/p>\n<div class=\"speech\"> <\/p>\n<p>Fourscore and seven years ago our fathers brought forth <br \/>     on this continent a new nation, conceived in liberty, <br \/>     and dedicated to the proposition that all men are <br \/>     created equal.  <\/p>\n<p> <\/p>\n<p>Now we are engaged in a great civil war, testing whether <br \/>     that nation, or any nation so conceived and so dedicated, <br \/>     can long endure. We are met on a great battlefield of <br \/>     that war. We have come to dedicate a portion of that <br \/>     field as a final resting-place for those who here gave <br \/>     their lives that the nation might live. It is altogether <br \/>     fitting and proper that we should do this. But, in a <br \/>     larger sense, we cannot dedicate, we cannot consecrate, <br \/>     we cannot hallow, this ground.  <\/p>\n<p> <span style=\"font-weight: bold;\"> <a href=\"#\" class=\"more\">read more <\/a> <\/span>  <\/div>\n<p>  <\/p>\n<p>When the DOM is ready, the second paragraph is hidden: <\/p>\n<pre style=\"margin-left: 20px;\">$(document).ready(function() { <br\/>  $('p:eq(1)').hide(); <br\/>}); <br\/> <\/pre>\n<p>And the speech looks like the following screenshot: <\/p>\n<p style=\"text-align: center;\"> <img decoding=\"async\" data-src=\"http:\/\/www.packtpub.com\/files\/images\/jquery_article1_image03.png\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" \/> <\/p>\n<p>Then, when the user clicks on <b>read more <\/b> at the end of the first paragraph, that link is hidden and the second paragraph is shown: <\/p>\n<pre style=\"margin-left: 20px;\">$(document).ready(function() { <br\/>  $('p:eq(1)').hide(); <br\/> <span style=\"font-weight: bold;\">$('a.more').click(function() { <\/span> <br style=\"font-weight: bold;\"\/> <span style=\"font-weight: bold;\">    $('p:eq(1)').show(); <\/span> <br style=\"font-weight: bold;\"\/> <span style=\"font-weight: bold;\">    $(this).hide(); <\/span> <br style=\"font-weight: bold;\"\/> <span style=\"font-weight: bold;\">    return false; <\/span> <br style=\"font-weight: bold;\"\/> <span style=\"font-weight: bold;\">  }); <\/span> <br\/>}); <br\/> <\/pre>\n<p>Note the use of return false to keep the link from activating its default action. Now the speech looks like this: <\/p>\n<p style=\"text-align: center;\"> <img decoding=\"async\" data-src=\"http:\/\/www.packtpub.com\/files\/images\/jquery_article1_image04.png\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" \/> <\/p>\n<p>The <i>.hide() <\/i> and <i>.show() <\/i> methods are quick and useful, but they aren&#8217;t very flashy. To add some flair, we can give them a speed. <\/p>\n<h2>Effects and speed <\/h2>\n<p>When we include a <b>speed <\/b> (or, more precisely, a <b>duration <\/b>) with <i>.show() <\/i> or <i>.hide() <\/i>, it becomes animated&#8212;occurring over a specified period of time. The <i>.hide(&#8216;speed&#8217;) <\/i> method, for example, decreases an element&#8217;s height, width, and opacity simultaneously until all three reach zero, at which point the CSS rule <i>display:none <\/i> is applied. The <i>.show(&#8216;speed&#8217;) <\/i> method will increase the element&#8217;s height from top to bottom, width from left to right, and opacity from 0 to 1 until its contents are completely visible. <\/p>\n<h2>Speeding in <\/h2>\n<p>With any jQuery effect, we can use one of three preset speeds: <i>&#8216;slow&#8217; <\/i>, <i>&#8216;normal&#8217; <\/i>, and <i>&#8216;fast&#8217; <\/i>. Using <i>.show(&#8216;slow&#8217;) <\/i> makes the show effect complete in .6 seconds, <i>.show(&#8216;normal&#8217;) <\/i> in .4 seconds, and <i>.show(&#8216;fast&#8217;) <\/i> in .2 seconds. For even greater precision we can specify a number of milliseconds, for example <i>.show(850) <\/i>. Unlike the speed names, the numbers are not wrapped in quotation marks. <\/p>\n<p>Let&#8217;s include a speed in our example when showing the second paragraph of Lincoln&#8217;s <b>Gettysburg Address <\/b>: <\/p>\n<pre style=\"margin-left: 20px;\">$(document).ready(function() { <br\/>  $('p:eq(1)').hide(); <br\/>  $('a.more').click(function() { <br\/> <span style=\"font-weight: bold;\">$('p:eq(1)').show('slow'); <\/span> <br\/>    $(this).hide(); <br\/>    return false; <br\/>  }); <br\/>}); <br\/> <\/pre>\n<p>When we capture the paragraph&#8217;s appearance at roughly halfway through the effect, we see something like the following: <\/p>\n<p style=\"text-align: center;\"> <img decoding=\"async\" data-src=\"http:\/\/www.packtpub.com\/files\/images\/jquery_article1_image05.png\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" \/> <\/p>\n<h2>Fading in and fading out <\/h2>\n<p>While the animated <i>.show() <\/i> and <i>.hide() <\/i> methods are certainly flashy, they may at times be too much of a good thing. Fortunately, jQuery offers a couple other pre-built animations for a more subtle effect. For example, to have the whole paragraph appear just by gradually increasing the opacity, we can use <i>.fadeIn(&#8216;slow&#8217;) <\/i> instead: <\/p>\n<pre style=\"margin-left: 20px;\">$(document).ready(function() { <br\/>  $('p:eq(1)').hide(); <br\/>  $('a.more').click(function() { <br\/> <span style=\"font-weight: bold;\">$('p:eq(1)').fadeIn('slow'); <\/span> <br\/>    $(this).hide(); <br\/>    return false; <br\/>  }); <br\/>}); <br\/> <\/pre>\n<p>This time when we capture the paragraph&#8217;s appearance halfway, it&#8217;s seen as: <\/p>\n<p style=\"text-align: center;\"> <img decoding=\"async\" data-src=\"http:\/\/www.packtpub.com\/files\/images\/jquery_article1_image06.png\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" \/> <\/p>\n<p>The difference here is that the <i>.fadeIn() <\/i> effect starts by setting the dimensions of the paragraph so that the contents can simply fade into it. To gradually decrease the opacity we can use <i>.fadeOut() <\/i>. <\/p>\n<hr size=\"1\" noshade=\"noshade\" color=\"#ff9933\"\/> <\/p>\n<div class=\"header\">Learning jQuery 1.3 <\/div>\n<div style=\"line-height: 0.4em;\">&#160; <\/div>\n<table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\n<tbody>\n<tr>\n<td width=\"99\" valign=\"top\"> <a href=\"http:\/\/www.packtpub.com\/learning-jquery-1.3\/book\/mid\/240609o1fp7w\" target=\"_blank\"> <img decoding=\"async\" height=\"123\" width=\"99\" border=\"0\" data-src=\"http:\/\/images.packtpub.com\/images\/100x123\/1847196705.png\" alt=\"Learning jQuery 1.3\" class=\"left lazyload\" title=\"Learning jQuery 1.3\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 99px; --smush-placeholder-aspect-ratio: 99\/123;\" \/> <\/a> <\/td>\n<td valign=\"top\">\n<ul>\n<li>Better Interaction Design and Web Development with Simple JavaScript Techniques <\/li>\n<li>An introduction to jQuery that requires minimal programming experience <\/li>\n<li>Detailed solutions to specific client-side problems <\/li>\n<li>For web designers to create interactive elements for their designs <\/li>\n<li>For developers to create the best user interface for their web applications <\/li>\n<li>Packed with great examples, code, and clear explanations <\/li>\n<li>Revised and updated version of the first book to help you learn jQuery <\/li>\n<\/ul>\n<p>&#160;&#160; <a href=\"http:\/\/www.packtpub.com\/learning-jquery-1.3\/book\/mid\/240609o1fp7w\" target=\"_blank\">http:\/\/www.packtpub.com\/learning-jquery-1.3\/book <\/a> <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p> <\/p>\n<hr size=\"1\" noshade=\"noshade\" color=\"#ff9933\"\/>\n<h1>Compound effects <\/h1>\n<p>Sometimes we have a need to toggle the visibility of elements, rather than displaying them once as we did in the previous example. Toggling can be achieved by first checking the visibility of the matched elements and then attaching the appropriate method. Using the fade effects again, we can modify the example script to look like this: <\/p>\n<pre style=\"margin-left: 20px;\">$(document).ready(function() { <br\/>  var $firstPara = $('p:eq(1)'); <br\/>  $firstPara.hide(); <br\/>  $('a.more').click(function() { <br\/>    if ($firstPara.is(':hidden')) { <br\/>      $firstPara.fadeIn('slow'); <br\/>      $(this).text('read less'); <br\/>    } else { <br\/>      $firstPara.fadeOut('slow'); <br\/>      $(this).text('read more'); <br\/>    } <br\/>    return false; <br\/>  }); <br\/>}); <br\/> <\/pre>\n<p>As we did earlier in the article, we&#8217;re <b>caching <\/b> our selector here to avoid repeated DOM traversal. Notice, too, that we&#8217;re no longer hiding the clicked link; instead, we&#8217;re changing the its text. <\/p>\n<p>Using an <i>if else <\/i> statement is a perfectly reasonable way to toggle elements&#8217; visibility. But with jQuery&#8217;s <b>compound effects <\/b> we can leave the conditionals out of it (although, in this example, we still need one for the link text). jQuery provides a <i>.toggle() <\/i>method, which acts like <i>.show() <\/i> and <i>.hide() <\/i>, and like them, can be used with a speed argument or without. The other compound method is <i>.slideToggle() <\/i>, which shows or hides elements by gradually increasing or decreasing their height. Here is what the script looks like when we use the <i>.slideToggle() <\/i> method: <\/p>\n<pre style=\"margin-left: 20px;\">$(document).ready(function() { <br\/>var $firstPara = $('p:eq(1)'); <br\/>  $firstPara.hide(); <br\/>  $('a.more').click(function() { <br\/> <span style=\"font-weight: bold;\">$firstPara.slideToggle('slow'); <\/span> <br style=\"font-weight: bold;\"\/> <span style=\"font-weight: bold;\">    var $link = $(this); <\/span> <br style=\"font-weight: bold;\"\/> <span style=\"font-weight: bold;\">    if ( $link.text() == \"read more\" ) { <\/span> <br style=\"font-weight: bold;\"\/> <span style=\"font-weight: bold;\">      $link.text('read less'); <\/span> <br style=\"font-weight: bold;\"\/> <span style=\"font-weight: bold;\">    } else { <\/span> <br style=\"font-weight: bold;\"\/> <span style=\"font-weight: bold;\">      $link.text('read more'); <\/span> <br style=\"font-weight: bold;\"\/> <span style=\"font-weight: bold;\">    } <\/span> <br\/>    return false; <br\/>  }); <br\/>}); <br\/> <\/pre>\n<p>This time <i>$(this) <\/i> would have been repeated, so we&#8217;re storing it in the $link variable for performance and readability. Also, the conditional statement checks for the text of the link rather than the visibility of the second paragraph, since we&#8217;re only using it to change the text. <\/p>\n<h1>Creating custom animations <\/h1>\n<p>In addition to the pre-built effect methods, jQuery provides a powerful <i>.animate() <\/i> method that allows us to create our own <b>custom animations <\/b> with fine-grained control. The <i>.animate() <\/i> method comes in two forms. The first takes up to four arguments: <\/p>\n<ol>\n<li>A <b>map <\/b> of style properties and values&#8212;similar to the <i>.css() <\/i> map discussed earlier in this article <\/li>\n<li>An optional <b>speed <\/b>&#8212;which can be one of the preset strings or a number of milliseconds <\/li>\n<li>An optional <b>easing type <\/b>&#8212;an advanced option <\/li>\n<li>An optional callback function&#8212;which will be discussed later in this article <\/li>\n<\/ol>\n<p>All together, the four arguments look like this: <\/p>\n<pre style=\"margin-left: 20px;\">.animate({property1: 'value1', property2: 'value2'}, <br\/>  speed, easing, function() { <br\/>    alert('The animation is finished.'); <br\/>  } <br\/>); <br\/> <\/pre>\n<p>The second form takes two arguments, a <strong>map <\/strong> of properties and a map of options. <\/p>\n<pre style=\"margin-left: 20px;\">.animate({properties}, {options}) <br\/> <\/pre>\n<p>In effect, the second argument wraps up the second through fourth arguments of the first form into another map, and adds two more options to the mix. When we adjust the line breaks for readability, the second form looks like this: <\/p>\n<pre style=\"margin-left: 20px;\">.animate({ <br\/>  property1: 'value1', <br\/>  property2: 'value2' <br\/>}, { <br\/>  duration: 'value', <br\/>  easing: 'value', <br\/>  complete: function() { <br\/>    alert('The animation is finished.'); <br\/>  }, <br\/>  queue: boolean, <br\/>  step: callback <br\/>}); <br\/> <\/pre>\n<p>For now we&#8217;ll use the first form of the <i>.animate() <\/i> method, but we&#8217;ll return to the second form later in the article when we discuss queuing effects. <\/p>\n<h2>Toggling the fade <\/h2>\n<p>When we discussed compound effects, did you notice that not all methods have a corresponding method for toggling? That&#8217;s right: while the sliding methods include <i>.slideToggle() <\/i>, there is no corresponding <i>.fadeToggle() <\/i> to go along with <i>.fadeIn() <\/i> and <i>.fadeOut() <\/i>! The good news is that we can use the <i>.animate() <\/i> method to easily make our own toggling fade animation. Here we&#8217;ll replace the <i>.slideToggle() <\/i> line of the previous example with our custom animation: <\/p>\n<pre style=\"margin-left: 20px;\">$(document).ready(function() { <br\/>  $('p:eq(1)').hide(); <br\/>  $('a.more').click(function() { <br\/> <span style=\"font-weight: bold;\"> $('p:eq(1)').animate({opacity: 'toggle'}, 'slow'); <\/span> <br\/>    var $link = $(this); <br\/>    if ( $link.text() == \"read more\" ) { <br\/>      $link.text('read less'); <br\/>    } else { <br\/>      $link.text('read more'); <br\/>    } <br\/>    return false; <br\/>  }); <br\/>}); <br\/> <\/pre>\n<p>As the example illustrates, the <i>.animate() <\/i> method provides convenient shorthand values for CSS properties &#8212; <i>&#8216;show&#8217; <\/i>, <i>&#8216;hide&#8217; <\/i>, and <i>&#8216;toggle&#8217; <\/i> &#8212; to ease the way when the shorthand <em>methods <\/em> aren&#8217;t quite right for the particular task. <\/p>\n<h2>Animating multiple properties <\/h2>\n<p>With the <i>.animate() <\/i> method, we can modify any combination of properties simultaneously. For example, to create a simultaneous sliding and fading effect when toggling the second paragraph, we simply add the <i>height <\/i> property-value pair to <i>.animate() <\/i>&#8216;s properties map: <\/p>\n<pre style=\"margin-left: 20px;\">$(document).ready(function() { <br\/>  $('p:eq(1)').hide(); <br\/>  $('a.more').click(function() { <br\/> <span style=\"font-weight: bold;\">$('p:eq(1)').animate({ <\/span> <br style=\"font-weight: bold;\"\/> <span style=\"font-weight: bold;\">      opacity: 'toggle', <\/span> <br style=\"font-weight: bold;\"\/> <span style=\"font-weight: bold;\">      height: 'toggle' <\/span> <br style=\"font-weight: bold;\"\/> <span style=\"font-weight: bold;\">    }, <\/span> <br style=\"font-weight: bold;\"\/> <span style=\"font-weight: bold;\">    'slow'); <\/span> <br\/>    var $link = $(this); <br\/>    if ( $link.text() == \"read more\" ) { <br\/>      $link.text('read less'); <br\/>    } else { <br\/>      $link.text('read more'); <br\/>    } <br\/>    return false; <br\/>  }); <br\/>}); <br\/> <\/pre>\n<p>Additionally, we have not only the style properties used for the shorthand effect methods at our disposal, but also other properties such as: <i>left <\/i>, <i>top <\/i>, <i>fontSize <\/i>, <i>margin <\/i>, <i>padding <\/i>, and <i>borderWidth <\/i>. Recall the script to change the text size of the speech paragraphs. We can animate the increase or decrease in size by simply substituting the <i>.animate() <\/i> method for the <i>.css() <\/i> method: <\/p>\n<pre style=\"margin-left: 20px;\">$(document).ready(function() { <br\/>  var $speech = $('div.speech'); <br\/>  var defaultSize = $speech.css('fontSize'); <br\/>  $('#switcher button').click(function() { <br\/>    var num = parseFloat( $speech.css('fontSize'), 10 ); <br\/>    switch (this.id) { <br\/>      case 'switcher-large': <br\/>        num *= 1.4; <br\/>        break; <br\/>      case 'switcher-small': <br\/>        num \/= 1.4; <br\/>        break; <br\/>      default: <br\/>        num = parseFloat(defaultSize, 10); <br\/>    } <br\/>    $speech.animate({fontSize: num + 'px'}, <br\/>                                    'slow'); <br\/>  }); <br\/>}); <br\/> <\/pre>\n<p>The extra properties allow us to create much more complex effects, too. We can, for example, move an item from the left side of the page to the right while increasing its height by 20 pixels and changing its border width to 5 pixels. <\/p>\n<p>So, let&#8217;s do that with the   box. Here is what it looks like before we animate it: <\/p>\n<p style=\"text-align: center;\"> <img decoding=\"async\" data-src=\"http:\/\/www.packtpub.com\/files\/images\/jquery_article1_image07.png\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" \/> <\/p>\n<p>With a flexible-width layout, we need to compute the distance that the box needs to travel before it lines up at the right side of the page. Assuming that the paragraph&#8217;s width is 100%, we can subtract the <b>Text Size <\/b> box&#8217;s width from the paragraph&#8217;s width. While jQuery&#8217;s <i>.width() <\/i> method would usually come in handy for such calculations, it doesn&#8217;t factor in the width of the right and left padding or the right and left border. As of jQuery version 1.2.6, though we also have the <i>.outerWidth() <\/i> method at our disposal. This is what we&#8217;ll use here, to avoid having to add padding and border widths as well. For the sake of this example, we&#8217;ll trigger the animation by clicking the <b>Text Size <\/b> label, just above the buttons. Here is what the code should look like: <\/p>\n<pre style=\"margin-left: 20px;\">$(document).ready(function() { <br\/>  $('div.label').click(function() { <br\/>    var paraWidth = $('div.speech p').outerWidth(); <br\/>    var $switcher = $(this).parent(); <br\/>    var switcherWidth = $switcher.outerWidth(); <br\/>    $switcher.animate({left: paraWidth - switcherWidth, <br\/>             height: '+=20px', borderWidth: '5px'}, 'slow'); <br\/>  }); <br\/>}); <br\/> <\/pre>\n<p>Note that the <i>height <\/i> property has <i>+= <\/i> before the pixel value. This expression, introduced in jQuery 1.2, indicates a <b>relative value <\/b>. So, instead of animating the height to 20 pixels, the height is animated to 20 pixels greater than the current height. <\/p>\n<p>Although this code successfully increases the height of the  and widens its border, at the moment the <i>left <\/i> position cannot be changed. We still need to enable changing its position in the CSS. <\/p>\n<h3>Positioning with CSS <\/h3>\n<p>When working with <i>.animate() <\/i>, it&#8217;s important to keep in mind the limitations that CSS imposes on the elements that we wish to change. For example, adjusting the <i>left <\/i> property will have no effect on the matching elements unless those elements have their CSS position set to <i>relative <\/i> or <i>absolute <\/i>. The default CSS position for all block-level elements is static, which accurately describes how those elements will remain if we try to move them without first changing their <i>position <\/i> value. <\/p>\n<p style=\"margin-left: 20px; margin-right: 20px;\"> <em>For more information on absolute and relative positioning, see Joe Gillespie&#8217;s article, Absolutely Relative at: <a href=\"http:\/\/www.wpdfd.com\/issues\/78\/absolutely_relative\/\" target=\"_blank\">http:\/\/www.wpdfd.com\/issues\/78\/absolutely_relative\/ <\/a> <\/em> <\/p>\n<p>A peek at our stylesheet shows that we have now set  to be relatively positioned: <\/p>\n<pre style=\"margin-left: 20px;\"> #switcher { <br\/>  position: relative; <br\/>} <br\/> <\/pre>\n<p>With the CSS taken into account, the result of clicking on Text Size, when the animation has completed, will look like this: <\/p>\n<p style=\"text-align: center;\"> <img decoding=\"async\" data-src=\"http:\/\/www.packtpub.com\/files\/images\/jquery_article1_image08.png\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" \/> <\/p>\n<hr size=\"1\" noshade=\"noshade\" color=\"#ff9933\"\/> <\/p>\n<div class=\"header\">Learning jQuery 1.3 <\/div>\n<div style=\"line-height: 0.4em;\">&#160; <\/div>\n<table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\n<tbody>\n<tr>\n<td width=\"99\" valign=\"top\"> <a href=\"http:\/\/www.packtpub.com\/learning-jquery-1.3\/book\/mid\/240609o1fp7w\" target=\"_blank\"> <img decoding=\"async\" height=\"123\" width=\"99\" border=\"0\" data-src=\"http:\/\/images.packtpub.com\/images\/100x123\/1847196705.png\" alt=\"Learning jQuery 1.3\" class=\"left lazyload\" title=\"Learning jQuery 1.3\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 99px; --smush-placeholder-aspect-ratio: 99\/123;\" \/> <\/a> <\/td>\n<td valign=\"top\">\n<ul>\n<li>Better Interaction Design and Web Development with Simple JavaScript Techniques <\/li>\n<li>An introduction to jQuery that requires minimal programming experience <\/li>\n<li>Detailed solutions to specific client-side problems <\/li>\n<li>For web designers to create interactive elements for their designs <\/li>\n<li>For developers to create the best user interface for their web applications <\/li>\n<li>Packed with great examples, code, and clear explanations <\/li>\n<li>Revised and updated version of the first book to help you learn jQuery <\/li>\n<\/ul>\n<p>&#160;&#160; <a href=\"http:\/\/www.packtpub.com\/learning-jquery-1.3\/book\/mid\/240609o1fp7w\" target=\"_blank\">http:\/\/www.packtpub.com\/learning-jquery-1.3\/book <\/a> <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p> <\/p>\n<hr size=\"1\" noshade=\"noshade\" color=\"#ff9933\"\/>\n<p>   <!-- END PART 2 --><\/p>\n<p>If you feel confident in what you just read, then go ahead and move on to <a href=\"https:\/\/www.andysowards.com\/blog\/tutorials\/how-to-add-flair-to-your-actions-with-jquery-%e2%80%93-part-3-%e2%80%93-javascript-tutorial\/\">Part 3<\/a>! Of course, it always helps to read things a second or third time first, just to let it sink in. Good luck! I hope you are learning some cool tricks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome back to part 2 of Adding Flair to your Actions with jQuery! If you missed Part 1, go ahead and read that first, or just move ahead! Basic hide<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[73,202,844,34],"tags":[2577,1967,2579,2575,110,5889,5896,433,2546,2581,2580,2576,2578,84],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Add Flair to your Actions with jQuery - Part 2 - Javascript Tutorial<\/title>\n<meta name=\"description\" content=\"Welcome back to part 2 of Adding Flair to your Actions with jQuery! If you missed Part 1, go ahead and read that first, or just move ahead! Basic hide and\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Add Flair to your Actions with jQuery - Part 2 - Javascript Tutorial\" \/>\n<meta property=\"og:description\" content=\"Welcome back to part 2 of Adding Flair to your Actions with jQuery! If you missed Part 1, go ahead and read that first, or just move ahead! Basic hide and\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"Daily Business Resources for Entrepreneurs, Web Designers, &amp; Creatives by Andy Sowards\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/andysowardsfan\" \/>\n<meta property=\"article:published_time\" content=\"2009-10-10T01:03:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2009-10-22T23:04:49+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.packtpub.com\/files\/images\/jquery_article1_image03.png\" \/>\n<meta name=\"author\" content=\"Packt Publishing\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@andysowards\" \/>\n<meta name=\"twitter:site\" content=\"@andysowards\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Packt Publishing\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/\",\"url\":\"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/\",\"name\":\"How to Add Flair to your Actions with jQuery - Part 2 - Javascript Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.andysowards.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/www.packtpub.com\/files\/images\/jquery_article1_image03.png\",\"datePublished\":\"2009-10-10T01:03:43+00:00\",\"dateModified\":\"2009-10-22T23:04:49+00:00\",\"author\":{\"@id\":\"https:\/\/www.andysowards.com\/blog\/#\/schema\/person\/cd18456865c95a17893e596aa9f4ee1e\"},\"description\":\"Welcome back to part 2 of Adding Flair to your Actions with jQuery! If you missed Part 1, go ahead and read that first, or just move ahead! Basic hide and\",\"breadcrumb\":{\"@id\":\"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/#primaryimage\",\"url\":\"http:\/\/www.packtpub.com\/files\/images\/jquery_article1_image03.png\",\"contentUrl\":\"http:\/\/www.packtpub.com\/files\/images\/jquery_article1_image03.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.andysowards.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Add Flair to your Actions with jQuery &#8211; Part 2 &#8211; Javascript Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.andysowards.com\/blog\/#website\",\"url\":\"https:\/\/www.andysowards.com\/blog\/\",\"name\":\"Daily Business Resources for Entrepreneurs, Web Designers, &amp; Creatives by Andy Sowards\",\"description\":\"Design Inspiration &amp; Business Resources for Creatives\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.andysowards.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.andysowards.com\/blog\/#\/schema\/person\/cd18456865c95a17893e596aa9f4ee1e\",\"name\":\"Packt Publishing\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.andysowards.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ea4b355a08dfdf148305e48ae3394064?s=96&r=pg\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ea4b355a08dfdf148305e48ae3394064?s=96&r=pg\",\"caption\":\"Packt Publishing\"},\"description\":\"Hi, I'm Jude from Packt Publishing. We have a wide range of books covering various technologies such as Open Source and Enterprise. http:\/\/www.packtpub.com\/\",\"sameAs\":[\"http:\/\/www.packtpub.com\/\"],\"url\":\"https:\/\/www.andysowards.com\/blog\/author\/packtpublishing\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Add Flair to your Actions with jQuery - Part 2 - Javascript Tutorial","description":"Welcome back to part 2 of Adding Flair to your Actions with jQuery! If you missed Part 1, go ahead and read that first, or just move ahead! Basic hide and","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"How to Add Flair to your Actions with jQuery - Part 2 - Javascript Tutorial","og_description":"Welcome back to part 2 of Adding Flair to your Actions with jQuery! If you missed Part 1, go ahead and read that first, or just move ahead! Basic hide and","og_url":"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/","og_site_name":"Daily Business Resources for Entrepreneurs, Web Designers, &amp; Creatives by Andy Sowards","article_publisher":"http:\/\/facebook.com\/andysowardsfan","article_published_time":"2009-10-10T01:03:43+00:00","article_modified_time":"2009-10-22T23:04:49+00:00","og_image":[{"url":"http:\/\/www.packtpub.com\/files\/images\/jquery_article1_image03.png"}],"author":"Packt Publishing","twitter_card":"summary_large_image","twitter_creator":"@andysowards","twitter_site":"@andysowards","twitter_misc":{"Written by":"Packt Publishing","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/","url":"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/","name":"How to Add Flair to your Actions with jQuery - Part 2 - Javascript Tutorial","isPartOf":{"@id":"https:\/\/www.andysowards.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/#primaryimage"},"thumbnailUrl":"http:\/\/www.packtpub.com\/files\/images\/jquery_article1_image03.png","datePublished":"2009-10-10T01:03:43+00:00","dateModified":"2009-10-22T23:04:49+00:00","author":{"@id":"https:\/\/www.andysowards.com\/blog\/#\/schema\/person\/cd18456865c95a17893e596aa9f4ee1e"},"description":"Welcome back to part 2 of Adding Flair to your Actions with jQuery! If you missed Part 1, go ahead and read that first, or just move ahead! Basic hide and","breadcrumb":{"@id":"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/#primaryimage","url":"http:\/\/www.packtpub.com\/files\/images\/jquery_article1_image03.png","contentUrl":"http:\/\/www.packtpub.com\/files\/images\/jquery_article1_image03.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.andysowards.com\/blog\/2009\/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.andysowards.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Add Flair to your Actions with jQuery &#8211; Part 2 &#8211; Javascript Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/www.andysowards.com\/blog\/#website","url":"https:\/\/www.andysowards.com\/blog\/","name":"Daily Business Resources for Entrepreneurs, Web Designers, &amp; Creatives by Andy Sowards","description":"Design Inspiration &amp; Business Resources for Creatives","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.andysowards.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.andysowards.com\/blog\/#\/schema\/person\/cd18456865c95a17893e596aa9f4ee1e","name":"Packt Publishing","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.andysowards.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ea4b355a08dfdf148305e48ae3394064?s=96&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ea4b355a08dfdf148305e48ae3394064?s=96&r=pg","caption":"Packt Publishing"},"description":"Hi, I'm Jude from Packt Publishing. We have a wide range of books covering various technologies such as Open Source and Enterprise. http:\/\/www.packtpub.com\/","sameAs":["http:\/\/www.packtpub.com\/"],"url":"https:\/\/www.andysowards.com\/blog\/author\/packtpublishing\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.andysowards.com\/blog\/wp-json\/wp\/v2\/posts\/2003"}],"collection":[{"href":"https:\/\/www.andysowards.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.andysowards.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.andysowards.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.andysowards.com\/blog\/wp-json\/wp\/v2\/comments?post=2003"}],"version-history":[{"count":0,"href":"https:\/\/www.andysowards.com\/blog\/wp-json\/wp\/v2\/posts\/2003\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.andysowards.com\/blog\/wp-json\/wp\/v2\/media?parent=2003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.andysowards.com\/blog\/wp-json\/wp\/v2\/categories?post=2003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.andysowards.com\/blog\/wp-json\/wp\/v2\/tags?post=2003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}