@@ -9,141 +9,141 @@ jQuery can show or hide content instantaneously with `$.fn.show` or `$.fn.hide`:
9
9
10
10
```
11
11
// Instantaneously hide all paragraphs
12
- $("p").hide();
12
+ $( "p" ).hide();
13
13
14
14
// Instantaneously show all divs that have the hidden style class
15
- $("div.hidden").show();
15
+ $( "div.hidden" ).show();
16
16
```
17
17
18
- When jQuery hides an element, it sets its CSS ` display ` property to ` none ` . This means the content will have
18
+ When jQuery hides an element, it sets its CSS ` display ` property to ` none ` . This means the content will have
19
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
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
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
23
in a string-valued argument of ` slow ` , ` normal ` , or ` fast ` :
24
24
25
25
```
26
26
// Slowly hide all paragraphs
27
- $("p").hide("slow");
27
+ $( "p" ).hide( "slow" );
28
28
29
29
// Quickly show all divs that have the hidden style class
30
- $("div.hidden").show("fast");
30
+ $( "div.hidden" ).show( "fast" );
31
31
```
32
32
33
- If you prefer more direct control over the duration of the animation effect, you
33
+ If you prefer more direct control over the duration of the animation effect, you
34
34
can pass the desired duration in milliseconds to ` $.fn.show ` and ` $.fn.hide ` :
35
35
36
36
```
37
37
// Hide all paragraphs over half a second
38
- $("p").hide( 500 );
38
+ $( "p" ).hide( 500 );
39
39
40
40
// Show all divs that have the hidden style class over 1.25 seconds
41
- $("div.hidden").show( 1250 );
41
+ $( "div.hidden" ).show( 1250 );
42
42
```
43
43
44
44
Most developers pass in a number of milliseconds to have more precise control
45
45
over the duration.
46
46
47
47
##Fade and Slide Animations
48
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
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
53
rapidly making changes to an element's CSS ` height ` property.
54
54
55
55
```
56
56
// Hide all paragraphs using a slide up animation over 0.8 seconds
57
- $("p").slideUp( 800 );
57
+ $( "p" ).slideUp( 800 );
58
58
59
59
// Show all hidden divs using a slide down animation over 0.6 seconds
60
- $("div.hidden").slideDown( 600 );
61
- ```
60
+ $( "div.hidden" ).slideDown( 600 );
61
+ ```
62
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.
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
65
66
66
```
67
67
// Hide all paragraphs using a fade out animation over 1.5 seconds
68
- $("p").fadeOut( 1500 );
68
+ $( "p" ).fadeOut( 1500 );
69
69
70
70
// Show all hidden divs using a fade in animation over 0.75 seconds
71
- $("div.hidden").fadeIn( 750 );
72
- ```
71
+ $( "div.hidden" ).fadeIn( 750 );
72
+ ```
73
73
74
74
##Changing Display Based on Current Visibility State
75
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
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
78
same arguments to ` $.fn.toggle ` as you pass to any of the effects methods above.
79
79
80
80
```
81
81
// Instantaneously toggle the display of all paragraphs
82
- $("p").toggle();
82
+ $( "p" ).toggle();
83
83
84
84
// Slowly toggle the display of all images
85
- $("img").toggle("slow");
85
+ $( "img" ).toggle( "slow" );
86
86
87
87
// Toggle the display of all divs over 1.8 seconds
88
- $("div").toggle( 1800 );
88
+ $( "div" ).toggle( 1800 );
89
89
```
90
90
91
- ` $.fn.toggle ` will use a combination of slide and fade effects, just as ` $.fn.show ` and ` $.fn.hide ` do. You can
91
+ ` $.fn.toggle ` will use a combination of slide and fade effects, just as ` $.fn.show ` and ` $.fn.hide ` do. You can
92
92
toggle the display of content with just a slide or a fade using ` $.fn.slideToggle ` and ` $.fn.fadeToggle ` .
93
93
94
94
```
95
95
// Toggle the display of all ordered lists over 1 second using slide up/down animations
96
- $("ol").slideToggle( 1000 );
96
+ $( "ol" ).slideToggle( 1000 );
97
97
98
98
// Toggle the display of all blockquotes over 0.4 seconds using fade in/out animations
99
- $("blockquote").fadeToggle( 400 );
99
+ $( "blockquote" ).fadeToggle( 400 );
100
100
```
101
101
102
102
##Doing Something After an Animation Completes
103
103
104
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.
105
+ chain will wait until the animation runs to completion.
106
106
107
107
```
108
108
// Fade in all hidden paragraphs; then add a style class to them (not quite right)
109
- $("p.hidden").fadeIn( 750 ).addClass("lookAtMe");
109
+ $( "p.hidden" ).fadeIn( 750 ).addClass( "lookAtMe" );
110
110
```
111
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
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
116
getting started.
117
117
118
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:
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
121
122
122
```
123
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");
124
+ $( "p.hidden" ).fadeIn( 750, function() {
125
+ // this = DOM element which has just finished being animated
126
+ $( this ).addClass( "lookAtMe" );
127
127
});
128
128
```
129
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
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
133
testing whether your selection returned any elements; if not, you can just run the callback immediately.
134
134
135
135
```
136
136
// Run a callback even if there were no elements to animate
137
- var $someElement = $("#nonexistent");
137
+ var $someElement = $( "#nonexistent" );
138
138
139
139
var cb = function() {
140
- console.log("done!");
140
+ console.log( "done!" );
141
141
};
142
142
143
143
if ( $someElement.length ) {
144
- $someElement.fadeIn( 300, cb );
144
+ $someElement.fadeIn( 300, cb );
145
145
} else {
146
- cb();
146
+ cb();
147
147
}
148
148
```
149
149
@@ -153,41 +153,40 @@ jQuery provides some additional features for controlling your animations:
153
153
154
154
### ` $.fn.stop `
155
155
156
- ` $.fn.stop ` will immediately terminate all animations running on the elements in your selection. You might give
156
+ ` $.fn.stop ` will immediately terminate all animations running on the elements in your selection. You might give
157
157
end-users control over page animations by rigging a button they can click to stop the animations.
158
158
159
159
```
160
160
// Create a button to stop all animations on the page:
161
- $("input").attr({
162
- type: "button",
163
- value: "Stop All Animations"
161
+ $( "input" ).attr({
162
+ type: "button",
163
+ value: "Stop All Animations"
164
164
}).on( "click", function() {
165
- $( "body *").filter(":animated").stop();
165
+ $( "body *" ).filter( ":animated" ).stop();
166
166
}).appendTo( document.body );
167
167
```
168
168
169
169
### ` $.fn.delay `
170
170
171
- ` $.fn.delay ` is used to introduce a delay between successive animations. For example:
171
+ ` $.fn.delay ` is used to introduce a delay between successive animations. For example:
172
172
173
173
```
174
- // Hide all level 1 headings over half a second; then wait for 1.5 seconds
174
+ // Hide all level 1 headings over half a second; then wait for 1.5 seconds
175
175
// and reveal all level 1 headings over 0.3 seconds
176
- $("h1").hide( 500 ).delay( 1500 ).show( 300 );
176
+ $( "h1" ).hide( 500 ).delay( 1500 ).show( 300 );
177
177
```
178
178
179
179
### ` jQuery.fx `
180
180
181
- The ` jQuery.fx ` object has a number of properties that control how effects are implemented. ` jQuery.fx.speeds ` maps
182
- the ` slow ` , ` normal ` , and ` fast ` duration arguments mentioned above to a specific
183
- number of milliseconds. The default value of ` jQuery.fx.speeds ` is:
181
+ The ` jQuery.fx ` object has a number of properties that control how effects are implemented. ` jQuery.fx.speeds ` maps
182
+ the ` slow ` , ` normal ` , and ` fast ` duration arguments mentioned above to a specific
183
+ number of milliseconds. The default value of ` jQuery.fx.speeds ` is:
184
184
185
185
```
186
186
{
187
- slow: 600,
188
- fast: 200,
189
- // Default speed, used for "normal"
190
- _default: 400
187
+ slow: 600,
188
+ fast: 200,
189
+ _default: 400 // Default speed, used for "normal"
191
190
}
192
191
```
193
192
@@ -199,23 +198,23 @@ jQuery.fx.speeds.blazing = 100;
199
198
jQuery.fx.speeds.excruciating = 60000;
200
199
```
201
200
202
- ` jQuery.fx.interval ` controls the number of frames per second that are
203
- displayed in an animation. The default value is 13 milliseconds between
204
- successive frames. You can set this a lower value for faster browsers
205
- to make the animations run smoother. However this will mean more frames
206
- per second and thus a higher computational load for the browser, so you
201
+ ` jQuery.fx.interval ` controls the number of frames per second that is
202
+ displayed in an animation. The default value is 13 milliseconds between
203
+ successive frames. You can set this to a lower value for faster browsers
204
+ to make the animations run smoother. However this will mean more frames
205
+ per second and thus a higher computational load for the browser, so you
207
206
should be sure to test the performance implications of doing so thoroughly.
208
207
209
208
Finally, ` jQuery.fx.off ` can be set to true to disable all animations. Elements
210
- will immediately be set to the target final state instead. This can be
209
+ will immediately be set to the target final state instead. This can be
211
210
especially useful when dealing with older browsers; you also may want to
212
211
provide the option to disable all animations to your users.
213
212
214
213
```
215
- $("input").attr({
216
- type : "button",
217
- value : "Disable Animations"
218
- }).on( "click", function(){
219
- jQuery.fx.off = true;
214
+ $( "input" ).attr({
215
+ type: "button",
216
+ value: "Disable Animations"
217
+ }).on( "click", function() {
218
+ jQuery.fx.off = true;
220
219
}).appendTo( document.body );
221
220
```
0 commit comments