|
| 1 | +--- |
| 2 | +title: Introduction to Effects |
| 3 | +level: beginner |
| 4 | +--- |
| 5 | + |
| 6 | +## Showing and Hiding Content |
| 7 | + |
| 8 | +jQuery can show or hide content instantaneously with `$.fn.show` or `$.fn.hide`: |
| 9 | + |
| 10 | +``` |
| 11 | +// Instantaneously hide all paragraphs |
| 12 | +$('p').hide(); |
| 13 | +
|
| 14 | +// Instantaneously show all divs that have the hidden style class |
| 15 | +$('div.hidden').show(); |
| 16 | +``` |
| 17 | + |
| 18 | +When jQuery hides an element, it sets its CSS `display` property to `none`. This means the content will have |
| 19 | +zero width and height; it does not mean that the content will simply become transparent and leave an empty area on the page. |
| 20 | + |
| 21 | +jQuery can also show or hide content by means of animation effects. You can tell |
| 22 | +`$.fn.show` and `$.fn.hide` to use animation in a couple of ways. One is to pass |
| 23 | +in a string-valued argument of 'slow', 'normal', or 'fast': |
| 24 | + |
| 25 | +``` |
| 26 | +// Slowly hide all paragraphs |
| 27 | +$('p').hide('slow'); |
| 28 | +
|
| 29 | +// Quickly show all divs that have the hidden style class |
| 30 | +$('div.hidden').show('fast'); |
| 31 | +``` |
| 32 | + |
| 33 | +If you prefer more direct control over the duration of the animation effect, you |
| 34 | +can pass the desired duration in milliseconds to `$.fn.show` and `$.fn.hide`: |
| 35 | + |
| 36 | +``` |
| 37 | +// Hide all paragraphs over half a second |
| 38 | +$('p').hide(500); |
| 39 | +
|
| 40 | +// Show all divs that have the hidden style class over 1.25 seconds |
| 41 | +$('div.hidden').show(1250); |
| 42 | +``` |
| 43 | + |
| 44 | +Most developers pass in a number of milleseconds to have more precise control |
| 45 | +over the duration. |
| 46 | + |
| 47 | +##Fade and Slide Animations |
| 48 | + |
| 49 | +You may have noticed that `$.fn.show` and `$.fn.hide` use a combination of slide and fade effects |
| 50 | +when showing and hiding content in an animated way. If you would rather show or hide content with |
| 51 | +one effect or the other, there are additional methods that can help. `$.fn.slideDown` and `$.fn.slideUp` |
| 52 | +show and hide content, respectively, using only a slide effect. Slide animations are accomplished by |
| 53 | +rapidly making changes to an element's CSS `height` property. |
| 54 | + |
| 55 | +``` |
| 56 | +// Hide all paragraphs using a slide up animation over 0.8 seconds |
| 57 | +$('p').slideUp(800); |
| 58 | +
|
| 59 | +// Show all hidden divs using a slide down animation over 0.6 seconds |
| 60 | +$('div.hidden').slideDown(600); |
| 61 | +``` |
| 62 | + |
| 63 | +Similarly `$.fn.fadeIn` and `$.fn.fadeOut` show and hide content, respectively, by means of a fade |
| 64 | +animation. Fade animations involve rapidly making changes to an element's CSS `opacity` property. |
| 65 | + |
| 66 | +``` |
| 67 | +// Hide all paragraphs using a fade out animation over 1.5 seconds |
| 68 | +$('p').fadeOut(1500); |
| 69 | +
|
| 70 | +// Show all hidden divs using a fade in animation over 0.75 seconds |
| 71 | +$('div.hidden').fadeIn(750); |
| 72 | +``` |
| 73 | + |
| 74 | +##Changing Display Based on Current Visibility State |
| 75 | + |
| 76 | +jQuery can also let you change a content's visibility based on its current visibility state. `$.fn.toggle` |
| 77 | +will show content that is currently hidden and hide content that is currently visible. You can pass the |
| 78 | +same arguments to `$.fn.toggle` as you pass to any of the effects methods above. |
| 79 | + |
| 80 | +``` |
| 81 | +// Instantaneously toggle the display of all paragraphs |
| 82 | +$('p').toggle(); |
| 83 | +
|
| 84 | +// Slowly toggle the display of all images |
| 85 | +$('img').toggle('slow'); |
| 86 | +
|
| 87 | +// Toggle the display of all divs over 1.8 seconds |
| 88 | +$('div').toggle(1800); |
| 89 | +``` |
| 90 | + |
| 91 | +`$.fn.toggle` will use a combination of slide and fade effects, just as `$.fn.show` and `$.fn.hide` do. You can |
| 92 | +toggle the display of content with just a slide or a fade using `$.fn.slideToggle` and `$.fn.fadeToggle`. |
| 93 | + |
| 94 | +``` |
| 95 | +// Toggle the display of all ordered lists over 1 second using slide up/down animations |
| 96 | +$('ol').slideToggle(1000); |
| 97 | +
|
| 98 | +// Toggle the display of all blockquotes over 0.4 seconds using fade in/out animations |
| 99 | +$('blockquote').fadeToggle(400); |
| 100 | +``` |
| 101 | + |
| 102 | +##Doing Something After an Animation Completes |
| 103 | + |
| 104 | +A common mistake when implementing jQuery effects is assuming that the execution of the next method in your |
| 105 | +chain will wait until the animation runs to completion. |
| 106 | + |
| 107 | +``` |
| 108 | +// Fade in all hidden paragraphs; then add a style class to them (not quite right) |
| 109 | +$('p.hidden').fadeIn(750).addClass('lookAtMe'); |
| 110 | +``` |
| 111 | + |
| 112 | +It is important to realize that `$.fn.fadeIn` above only *kicks off* the animation. Once started, the |
| 113 | +animation is implemented by rapidly changing CSS properties in a JavaScript `setInterval()` loop. When |
| 114 | +you call `$.fn.fadeIn`, it starts the animation loop and then returns the jQuery object, passing it along |
| 115 | +to `$.fn.addClass` which will then add the `lookAtMe` style class while the animation loop is just |
| 116 | +getting started. |
| 117 | + |
| 118 | +To defer an action until after an animation has run to completion, you need to use an animation callback |
| 119 | +function. You can specify your animation callback as the second argument passed to any of the |
| 120 | +animation methods discussed above. For the code snippet above, we can implement a callback as follows: |
| 121 | + |
| 122 | +``` |
| 123 | +// Fade in all hidden paragraphs; then add a style class to them (correct with animation callback) |
| 124 | +$('p.hidden').fadeIn(750, function(){ |
| 125 | + // this = DOM element which has just finished being animated |
| 126 | + $(this).addClass('lookAtMe'); |
| 127 | +}); |
| 128 | +``` |
| 129 | + |
| 130 | +Note that you can use the keyword `this` to refer to the DOM element being animated. Also note |
| 131 | +that the callback will be called for each element in the jQuery object. This means that if your |
| 132 | +selector returns no elements, your animation callback will never run! You can solve this problem by |
| 133 | +testing whether your selection returned any elements; if not, you can just run the callback immediately. |
| 134 | + |
| 135 | +``` |
| 136 | +// Run a callback even if there were no elements to animate |
| 137 | +var $someElement = $('#nonexistent'); |
| 138 | +
|
| 139 | +var cb = function() { |
| 140 | + console.log('done!'); |
| 141 | +}; |
| 142 | +
|
| 143 | +if ($someElement.length) { |
| 144 | + $someElement.fadeIn(300, cb); |
| 145 | +} else { |
| 146 | + cb(); |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +##Managing Animation Effects |
| 151 | + |
| 152 | +jQuery provides some additional features for controlling your animations: |
| 153 | + |
| 154 | +### `$.fn.stop` |
| 155 | + |
| 156 | +`$.fn.stop` will immediately terminate all animations running on the elements in your selection. You might give |
| 157 | +end-users control over page animations by rigging a button they can click to stop the animations. |
| 158 | + |
| 159 | +``` |
| 160 | +// Create a button to stop all animations on the page: |
| 161 | +$('input').attr({type : 'button', value : 'Stop All Animations'}).on('click', function(){ |
| 162 | + $('body *').filter(':animated').stop(); |
| 163 | +}).appendTo(document.body); |
| 164 | +``` |
| 165 | + |
| 166 | +### `$.fn.delay` |
| 167 | + |
| 168 | +`$.fn.delay` is used to introduce a delay between successive animations. For example: |
| 169 | + |
| 170 | +``` |
| 171 | +// Hide all level 1 headings over half a second; then wait for 1.5 seconds |
| 172 | +// and reveal all level 1 headings over 0.3 seconds |
| 173 | +$('h1').hide(500).delay(1500).show(300); |
| 174 | +``` |
| 175 | + |
| 176 | +### `jQuery.fx` |
| 177 | + |
| 178 | +The `jQuery.fx` object has a number of properties that control how effects are implemented. `jQuery.fx.speeds` maps |
| 179 | +the 'slow', 'normal', and 'fast' duration arguments mentioned above to a specific |
| 180 | +number of milliseconds. The default value of `jQuery.fx.speeds` is: |
| 181 | + |
| 182 | +``` |
| 183 | +{ |
| 184 | + slow: 600, |
| 185 | + fast: 200, |
| 186 | + // Default speed, used for 'normal' |
| 187 | + _default: 400 |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +You can modify any of these settings and even introduce some of your own: |
| 192 | + |
| 193 | +``` |
| 194 | +jQuery.fx.speeds.fast = 300; |
| 195 | +jQuery.fx.speeds.blazing = 100; |
| 196 | +jQuery.fx.speeds.excruciating = 60000; |
| 197 | +``` |
| 198 | + |
| 199 | +`jQuery.fx.interval` controls the number of frames per second that are |
| 200 | +displayed in an animation. The default value is 13 milliseconds between |
| 201 | +successive frames. You can set this a lower value for faster browsers |
| 202 | +to make the animations run smoother. However this will mean more frames |
| 203 | +per second and thus a higher computational load for the browser, so you |
| 204 | +should be sure to test the performance implications of doing so thoroughly. |
| 205 | + |
| 206 | +Finally, `jQuery.fx.off` can be set to true to disable all animations. Elements |
| 207 | +will immediately be set to the target final state instead. This can be |
| 208 | +especially useful when dealing with older browsers; you also may want to |
| 209 | +provide the option to disable all animations to your users. |
| 210 | + |
| 211 | +``` |
| 212 | +$('input').attr({type : 'button', value : 'Disable Animations'}).on('click', function(){ |
| 213 | + jQuery.fx.off = true; |
| 214 | +}).appendTo(document.body); |
| 215 | +``` |
0 commit comments