1 line
20 KiB
JSON
1 line
20 KiB
JSON
{"name":"animate","type":"method","title":".animate()","deprecated":null,"removed":null,"desc":"Perform a custom animation of a set of CSS properties.","categories":["effects/custom-effects","version/1.0"],"entries":[{"return":"jQuery","signatures":[{"added":"1.0","argument":[{"desc":"An object of CSS properties and values that the animation will move toward.","name":"properties","type":"PlainObject"},{"desc":"A string or number determining how long the animation will run.","type":[{"name":"Number"},{"name":"String"}],"name":"duration","default":"400","optional":"true"},{"desc":"A string indicating which easing function to use for the transition.","name":"easing","type":"String","default":"swing","optional":"true"},{"desc":"A function to call once the animation is complete.","name":"complete","type":"Function","optional":"true"}]},{"added":"1.0","argument":[{"desc":"An object of CSS properties and values that the animation will move toward.","name":"properties","type":"PlainObject"},{"desc":"A map of additional options to pass to the method.","property":[{"desc":"A string or number determining how long the animation will run.","type":[{"name":"Number"},{"name":"String"}],"name":"duration","default":"400"},{"desc":"A string indicating which easing function to use for the transition.","name":"easing","type":"String","default":"swing"},{"desc":"A Boolean indicating whether to place the animation in the effects queue. If false, the animation will begin immediately. <strong>As of jQuery 1.7</strong>, the queue option can also accept a string, in which case the animation is added to the queue represented by that string. When a custom queue name is used the animation does not automatically start; you must call <code>.dequeue(\"queuename\")</code> to start it.","type":[{"name":"Boolean"},{"name":"String"}],"name":"queue","default":"true"},{"desc":"A map of one or more of the CSS properties defined by the properties argument and their corresponding easing functions.","name":"specialEasing","type":"PlainObject","added":"1.4"},{"desc":"A function to be called for each animated property of each animated element. This function provides an opportunity to modify the Tween object to change the value of the property before it is set.","argument":[{"desc":"The numeric value of the property being animated at each step","name":"now","type":"Number"},{"desc":"An object of properties related to the animation and the element being animated. For information about the tween object and its properties, see <ahref=\"/jQuery.Tween/\">jQuery.Tween</a>","name":"tween","type":"Tween"}],"name":"step","type":"Function"},{"desc":"A function to be called after each step of the animation, only once per animated element regardless of the number of animated properties.","argument":[{"desc":"An enhanced Promise object with additional properties for the animation","name":"animation","type":"Promise"},{"desc":"A number from 0 to 1 indicating the progress of the animation","name":"progress","type":"Number"},{"desc":"A number indicating the remaining number of milliseconds until the scheduled end of the animation","name":"remainingMs","type":"Number"}],"name":"progress","type":"Function","added":"1.8"},{"desc":"A function to call once the animation is complete.","name":"complete","type":"Function"},{"desc":"A function to call when the animation begins.","argument":{"desc":"An enhanced Promise object with additional properties for the animation","name":"animation","type":"Promise"},"name":"start","type":"Function","added":"1.8"},{"desc":"A function to be called when the animation completes (its Promise object is resolved).","argument":[{"desc":"An enhanced Promise object with additional properties for the animation","name":"animation","type":"Promise"},{"desc":"Indicates whether the animation jumped to the end","name":"jumpedToEnd","type":"Boolean"}],"name":"done","type":"Function","added":"1.8"},{"desc":"A function to be called when the animation fails to complete (its Promise object is rejected).","argument":[{"desc":"An enhanced Promise object with additional properties for the animation","name":"animation","type":"Promise"},{"desc":"Indicates whether the animation jumped to the end","name":"jumpedToEnd","type":"Boolean"}],"name":"fail","type":"Function","added":"1.8"},{"desc":"A function to be called when the animation completes or stops without completing (its Promise object is either resolved or rejected).","argument":[{"desc":"An enhanced Promise object with additional properties for the animation","name":"animation","type":"Promise"},{"desc":"Indicates whether the animation jumped to the end","name":"jumpedToEnd","type":"Boolean"}],"name":"always","type":"Function","added":"1.8"}],"name":"options","type":"PlainObject"}]}],"examples":[{"desc":"Click the button to animate the div with a number of different properties.","code":"\n// Using multiple unit types within one animation.\n\n$( \"#go\" ).click(function() {\n $( \"#block\" ).animate({\n width: \"70%\",\n opacity: 0.4,\n marginLeft: \"0.6in\",\n fontSize: \"3em\",\n borderWidth: \"10px\"\n }, 1500 );\n});\n","html":"\n<button id=\"go\">» Run</button>\n<div id=\"block\">Hello!</div>\n","css":"\n div {\n background-color: #bca;\n width: 100px;\n border: 1px solid green;\n }\n"},{"desc":"Animates a div's left property with a relative value. Click several times on the buttons to see the relative animations queued up.","code":"\n$( \"#right\" ).click(function() {\n $( \".block\" ).animate({ \"left\": \"+=50px\" }, \"slow\" );\n});\n\n$( \"#left\" ).click(function(){\n $( \".block\" ).animate({ \"left\": \"-=50px\" }, \"slow\" );\n});\n","html":"\n<button id=\"left\">«</button>\n<button id=\"right\">»</button>\n<div class=\"block\"></div>\n","css":"\n div {\n position: absolute;\n background-color: #abc;\n left: 50px;\n width: 90px;\n height: 90px;\n margin: 5px;\n }\n"},{"desc":"The first button shows how an unqueued animation works. It expands the div out to 90% width <strong>while</strong> the font-size is increasing. Once the font-size change is complete, the border animation will begin.\n\nThe second button starts a traditional chained animation, where each animation will start once the previous animation on the element has completed.","code":"\n$( \"#go1\" ).click(function() {\n $( \"#block1\" )\n .animate({\n width: \"90%\"\n }, {\n queue: false,\n duration: 3000\n })\n .animate({ fontSize: \"24px\" }, 1500 )\n .animate({ borderRightWidth: \"15px\" }, 1500 );\n});\n\n$( \"#go2\" ).click(function() {\n $( \"#block2\" )\n .animate({ width: \"90%\" }, 1000 )\n .animate({ fontSize: \"24px\" }, 1000 )\n .animate({ borderLeftWidth: \"15px\" }, 1000 );\n});\n\n$( \"#go3\" ).click(function() {\n $( \"#go1\" ).add( \"#go2\" ).click();\n});\n\n$( \"#go4\" ).click(function() {\n $( \"div\" ).css({\n width: \"\",\n fontSize: \"\",\n borderWidth: \"\"\n });\n});\n","html":"\n<button id=\"go1\">» Animate Block1</button>\n<button id=\"go2\">» Animate Block2</button>\n<button id=\"go3\">» Animate Both</button>\n<button id=\"go4\">» Reset</button>\n<div id=\"block1\">Block1</div>\n<div id=\"block2\">Block2</div>\n","css":"\n div {\n background-color: #bca;\n width: 200px;\n height: 1.1em;\n text-align: center;\n border: 2px solid green;\n margin: 3px;\n font-size: 14px;\n }\n button {\n font-size: 14px;\n }\n"},{"desc":"Animates the first div's left property and synchronizes the remaining divs, using the step function to set their left properties at each stage of the animation. ","code":"\n$( \"#go\" ).click(function() {\n $( \".block:first\" ).animate({\n left: 100\n }, {\n duration: 1000,\n step: function( now, fx ){\n $( \".block:gt(0)\" ).css( \"left\", now );\n }\n });\n});\n","css":"\n div {\n position: relative;\n background-color: #abc;\n width: 40px;\n height: 40px;\n float: left;\n margin: 5px;\n }\n","html":"\n<p><button id=\"go\">Run »</button></p>\n<div class=\"block\"></div>\n<div class=\"block\"></div>\n<div class=\"block\"></div>\n<div class=\"block\"></div>\n<div class=\"block\"></div>\n<div class=\"block\"></div>\n"},{"desc":"Animate all paragraphs to toggle both height and opacity, completing the animation within 600 milliseconds.","code":"\n$( \"p\" ).animate({\n height: \"toggle\",\n opacity: \"toggle\"\n}, \"slow\" );\n"},{"desc":"Animate all paragraphs to a left style of 50 and opacity of 1 (opaque, visible), completing the animation within 500 milliseconds.","code":"\n$( \"p\" ).animate({\n left: 50,\n opacity: 1\n}, 500 );\n"},{"desc":"Animate the left and opacity style properties of all paragraphs; run the animation <em>outside</em> the queue, so that it will automatically start without waiting for its turn.","code":"\n$( \"p\" ).animate({\n left: \"50px\",\n opacity: 1\n}, {\n duration: 500,\n queue: false\n});\n"},{"desc":"An example of using an 'easing' function to provide a different style of animation. This will only work if you have a plugin that provides this easing function. Note, this code will do nothing unless the paragraph element is hidden.","code":"\n$( \"p\" ).animate({\n opacity: \"show\"\n}, \"slow\", \"easein\" );\n"},{"desc":"Animates all paragraphs to toggle both height and opacity, completing the animation within 600 milliseconds.","code":"\n$( \"p\" ).animate({\n height: \"toggle\",\n opacity: \"toggle\"\n}, {\n duration: \"slow\"\n});\n"},{"desc":"Use an easing function to provide a different style of animation. This will only work if you have a plugin that provides this easing function.","code":"\n$( \"p\" ).animate({\n opacity: \"show\"\n}, {\n duration: \"slow\",\n easing: \"easein\"\n});\n"},{"desc":"Animate all paragraphs and execute a callback function when the animation is complete. The first argument is an object of CSS properties, the second specifies that the animation should take 1000 milliseconds to complete, the third states the easing type, and the fourth argument is an anonymous callback function. ","code":"\n$( \"p\" ).animate({\n height: 200,\n width: 400,\n opacity: 0.5\n}, 1000, \"linear\", function() {\n alert( \"all done\" );\n});\n"}],"longdesc":"\n <p>The <code>.animate()</code> method allows us to create animation effects on any numeric CSS property. The only required parameter is a plain object of CSS properties. This object is similar to the one that can be sent to the <code>.css()</code> method, except that the range of properties is more restrictive.</p>\n <h4 id=\"animation-properties\">Animation Properties and Values</h4>\n <p>All animated properties should be animated to a <em>single numeric value</em>, except as noted below; most properties that are non-numeric cannot be animated using basic jQuery functionality (For example, <code>width</code>, <code>height</code>, or <code>left</code> can be animated but <code>background-color</code> cannot be, unless the <a href=\"https://github.com/jquery/jquery-color\">jQuery.Color()</a> plugin is used). Property values are treated as a number of pixels unless otherwise specified. The units <code>em</code> and <code>%</code> can be specified where applicable.</p>\n <p>In addition to style properties, some non-style properties such as <code>scrollTop</code> and <code>scrollLeft</code>, as well as custom properties, can be animated.</p>\n <p>Shorthand CSS properties (e.g. font, background, border) are not fully supported. For example, if you want to animate the rendered border width, at least a border style and border width other than \"auto\" must be set in advance. Or, if you want to animate font size, you would use <code>fontSize</code> or the CSS equivalent <code>'font-size'</code> rather than simply <code>'font'</code>. </p>\n <p>In addition to numeric values, each property can take the strings <code>'show'</code>, <code>'hide'</code>, and <code>'toggle'</code>. These shortcuts allow for custom hiding and showing animations that take into account the display type of the element. In order to use jQuery's built-in toggle state tracking, the <code>'toggle'</code> keyword must be consistently given as the value of the property being animated.</p>\n <p>Animated properties can also be relative. If a value is supplied with a leading <code>+=</code> or <code>-=</code> sequence of characters, then the target value is computed by adding or subtracting the given number from the current value of the property.</p>\n <div class=\"warning\">\n <p><strong>Note:</strong> Unlike shorthand animation methods such as <code>.slideDown()</code> and <code>.fadeIn()</code>, the <code>.animate()</code> method does <em>not</em> make hidden elements visible as part of the effect. For example, given <code>$( \"someElement\" ).hide().animate({height: \"20px\"}, 500)</code>, the animation will run, but <em>the element will remain hidden</em>.</p>\n </div>\n <h4 id=\"duration\">Duration</h4>\n <p>Durations are given in milliseconds; higher values indicate slower animations, not faster ones. The default duration is <code>400</code> milliseconds. The strings <code>'fast'</code> and <code>'slow'</code> can be supplied to indicate durations of <code>200</code> and <code>600</code> milliseconds, respectively.</p>\n <h4 id=\"complete\">Complete Function</h4>\n <p>If supplied, the <code>complete</code> callback function is fired once the animation is complete. This can be useful for stringing different animations together in sequence. The callback is not sent any arguments, but <code>this</code> is set to the DOM element being animated. If multiple elements are animated, the callback is executed once per matched element, not once for the animation as a whole.</p>\n <h4 id=\"basic-usage\">Basic Usage</h4>\n <p>To animate any element, such as a simple image:</p>\n <pre><code>\n<div id=\"clickme\">\n Click here\n</div>\n<img id=\"book\" src=\"book.png\" alt=\"\" width=\"100\" height=\"123\"\n style=\"position: relative; left: 10px;\">\n </code></pre>\n <p>To animate the opacity, left offset, and height of the image simultaneously:</p>\n <pre><code>\n$( \"#clickme\" ).click(function() {\n $( \"#book\" ).animate({\n opacity: 0.25,\n left: \"+=50\",\n height: \"toggle\"\n }, 5000, function() {\n // Animation complete.\n });\n});\n </code></pre>\n <p class=\"image\">\n <img src=\"/resources/animate-1.jpg\" alt=\"\"/>\n </p>\n <p>Note that the target value of the <code>height</code> property is <code>'toggle'</code>. Since the image was visible before, the animation shrinks the height to 0 to hide it. A second click then reverses this transition:\n </p>\n <p class=\"image\">\n <img src=\"/resources/animate-2.jpg\" alt=\"\"/>\n </p>\n <p>The <code>opacity</code> of the image is already at its target value, so this property is not animated by the second click. Since the target value for <code>left</code> is a relative value, the image moves even farther to the right during this second animation.</p>\n <p>Directional properties (<code>top</code>, <code>right</code>, <code>bottom</code>, <code>left</code>) have no discernible effect on elements if their <code>position</code> style property is <code>static</code>, which it is by default.</p>\n <div class=\"warning\">\n <p><strong>Note: </strong>The <a href=\"http://jqueryui.com/\">jQuery UI</a> project extends the <code>.animate()</code> method by allowing some non-numeric styles such as colors to be animated. The project also includes mechanisms for specifying animations through CSS classes rather than individual attributes.</p>\n </div>\n <div class=\"warning\">\n <p><strong>Note:</strong> if attempting to animate an element with a height or width of 0px, where contents of the element are visible due to overflow, jQuery may clip this overflow during animation. By fixing the dimensions of the original element being hidden however, it is possible to ensure that the animation runs smoothly. A <a href=\"http://www.google.com/search?q=clearfix\">clearfix</a> can be used to automatically fix the dimensions of your main element without the need to set this manually.</p>\n </div>\n <h4 id=\"step\">Step Function</h4>\n <p>The second version of <code>.animate()</code> provides a <code>step</code> option — a callback function that is fired at each step of the animation. This function is useful for enabling custom animation types or altering the animation as it is occurring. It accepts two arguments (<code>now</code> and <code>fx</code>), and <code>this</code> is set to the DOM element being animated.</p>\n <ul>\n <li><code>now</code>: the numeric value of the property being animated at each step</li>\n <li><code>fx</code>: a reference to the <code>jQuery.fx</code> prototype object, which contains a number of properties such as <code>elem</code> for the animated element, <code>start</code> and <code>end</code> for the first and last value of the animated property, respectively, and <code>prop</code> for the property being animated.</li>\n </ul>\n <p>Note that the <code>step</code> function is called for each animated property on each animated element. For example, given two list items, the <code>step</code> function fires four times at each step of the animation: </p>\n <pre><code>\n$( \"li\" ).animate({\n opacity: .5,\n height: \"50%\"\n}, {\n step: function( now, fx ) {\n var data = fx.elem.id + \" \" + fx.prop + \": \" + now;\n $( \"body\" ).append( \"<div>\" + data + \"</div>\" );\n }\n});\n </code></pre>\n <h4 id=\"easing\">Easing</h4>\n <p>The remaining parameter of <code>.animate()</code> is a string naming an easing function to use. An easing function specifies the speed at which the animation progresses at different points within the animation. The only easing implementations in the jQuery library are the default, called <code>swing</code>, and one that progresses at a constant pace, called <code>linear</code>. More easing functions are available with the use of plug-ins, most notably the <a href=\"http://jqueryui.com/\">jQuery UI suite</a>.</p>\n <h4 id=\"per-property-easing\">Per-property Easing</h4>\n <p>As of jQuery version 1.4, you can set per-property easing functions within a single <code>.animate()</code> call. In the first version of <code>.animate()</code>, each property can take an array as its value: The first member of the array is the CSS property and the second member is an easing function. If a per-property easing function is not defined for a particular property, it uses the value of the <code>.animate()</code> method's optional easing argument. If the easing argument is not defined, the default <code>swing</code> function is used.</p>\n <p>For example, to simultaneously animate the width and height with the <code>swing</code> easing function and the opacity with the <code>linear</code> easing function:</p>\n <pre><code>\n$( \"#clickme\" ).click(function() {\n $( \"#book\" ).animate({\n width: [ \"toggle\", \"swing\" ],\n height: [ \"toggle\", \"swing\" ],\n opacity: \"toggle\"\n }, 5000, \"linear\", function() {\n $( this ).after( \"<div>Animation complete.</div>\" );\n });\n});\n </code></pre>\n <p>In the second version of <code>.animate()</code>, the options object can include the <code>specialEasing</code> property, which is itself an object of CSS properties and their corresponding easing functions. For example, to simultaneously animate the width using the <code>linear</code> easing function and the height using the <code>easeOutBounce</code> easing function:</p>\n <pre><code>\n$( \"#clickme\" ).click(function() {\n $( \"#book\" ).animate({\n width: \"toggle\",\n height: \"toggle\"\n }, {\n duration: 5000,\n specialEasing: {\n width: \"linear\",\n height: \"easeOutBounce\"\n },\n complete: function() {\n $( this ).after( \"<div>Animation complete.</div>\" );\n }\n });\n});\n </code></pre>\n <p>As previously noted, a plugin is required for the <code>easeOutBounce</code> function.</p>\n "}]} |