Should You Animate With Jquery Or Css
Description: Perform a custom animation of a fix of CSS properties.
The .animate()
method allows us to create animation furnishings on any numeric CSS property. The merely required parameter is a manifestly object of CSS properties. This object is similar to the one that can exist sent to the .css()
method, except that the range of properties is more restrictive.
Animation Properties and Values
All blithe properties should be animated to a single numeric value, except as noted beneath; nigh properties that are non-numeric cannot be animated using basic jQuery functionality (For example, width
, pinnacle
, or left
can exist blithe but background-color
cannot be, unless the jQuery.Color plugin is used). Belongings values are treated as a number of pixels unless otherwise specified. The units em
and %
can exist specified where applicable.
In addition to style properties, some non-manner properties such every bit scrollTop
and scrollLeft
, equally well as custom properties, can be animated.
Shorthand CSS properties (e.g. font, background, border) are not fully supported. For example, if you desire to animate the rendered border width, at least a edge style and edge width other than "auto" must exist gear up in advance. Or, if you want to animate font size, y'all would use fontSize
or the CSS equivalent 'font-size'
rather than simply 'font'
.
In improver to numeric values, each property can take the strings 'show'
, 'hide'
, and 'toggle'
. These shortcuts allow for custom hiding and showing animations that have into business relationship the display blazon of the element. In social club to use jQuery's congenital-in toggle state tracking, the 'toggle'
keyword must exist consistently given as the value of the holding being animated.
Blithe properties can also be relative. If a value is supplied with a leading +=
or -=
sequence of characters, and then the target value is computed by adding or subtracting the given number from the current value of the belongings.
Note: Unlike shorthand blitheness methods such as .slideDown()
and .fadeIn()
, the .animate()
method does not brand hidden elements visible as part of the consequence. For instance, given $( "someElement" ).hide().breathing({height: "20px"}, 500)
, the animation volition run, merely the element will remain hidden.
Duration
Durations are given in milliseconds; higher values indicate slower animations, not faster ones. The default elapsing is 400
milliseconds. The strings 'fast'
and 'slow'
can be supplied to indicate durations of 200
and 600
milliseconds, respectively.
Callback Functions
If supplied, the start
, footstep
, progress
, complete
, done
, fail
, and ever
callbacks are called on a per-element basis; this
is set to the DOM element being blithe. If no elements are in the set, no callbacks are called. If multiple elements are blithe, the callback is executed once per matched element, not one time for the animation every bit a whole. Use the .promise()
method to obtain a promise to which you can attach callbacks that fire once for an blithe prepare of whatever size, including nix elements.
Basic Usage
To animate any element, such as a uncomplicated prototype:
1 two three 4 v | |
To breathing the opacity, left offset, and height of the prototype simultaneously:
i 2 iii iv 5 6 7 8 9 | |
Note that the target value of the pinnacle
property is 'toggle'
. Since the paradigm was visible before, the animation shrinks the height to 0 to hibernate information technology. A second click then reverses this transition:
The opacity
of the image is already at its target value, and so this belongings is non animated by the 2d click. Since the target value for left
is a relative value, the image moves even farther to the right during this second blitheness.
Directional backdrop (pinnacle
, right
, bottom
, left
) have no discernible effect on elements if their position
manner property is static
, which it is by default.
Notation: The jQuery UI project extends the .animate()
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.
Note: 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 prune this overflow during animation. By fixing the dimensions of the original chemical element being hidden yet, information technology is possible to ensure that the animation runs smoothly. A clearfix tin be used to automatically fix the dimensions of your main element without the need to set this manually.
Step Part
The 2nd version of .animate()
provides a stride
option — a callback office that is fired at each step of the animation. This function is useful for enabling custom animation types or altering the blitheness as it is occurring. It accepts two arguments (now
and fx
), and this
is set to the DOM element beingness animated.
-
now
: the numeric value of the property being animated at each stride -
fx
: a reference to thejQuery.fx
prototype object, which contains a number of backdrop such aselem
for the animated chemical element,start
andfinish
for the starting time and last value of the animated holding, respectively, andprop
for the holding being animated.
Note that the footstep
function is called for each blithe property on each animated element. For example, given ii list items, the step
role fires four times at each footstep of the animation:
1 2 3 iv 5 6 7 8 nine | |
Easing
The remaining parameter of .animate()
is a string naming an easing office to use. An easing function specifies the speed at which the animation progresses at different points inside the blitheness. The only easing implementations in the jQuery library are the default, called swing
, and i that progresses at a abiding pace, chosen linear
. More easing functions are available with the use of plug-ins, nearly notably the jQuery UI suite.
Per-property Easing
Equally of jQuery version ane.iv, you tin can gear up per-holding easing functions within a single .animate()
telephone call. In the showtime version of .animate()
, each property can take an assortment every bit its value: The showtime member of the array is the CSS property and the second member is an easing role. If a per-holding easing part is not defined for a item property, information technology uses the value of the .animate()
method'due south optional easing statement. If the easing statement is non defined, the default swing
function is used.
For example, to simultaneously breathing the width and height with the swing
easing function and the opacity with the linear
easing role:
1 2 three 4 v vi 7 viii 9 | |
In the second version of .breathing()
, the options object tin can include the specialEasing
holding, which is itself an object of CSS properties and their corresponding easing functions. For instance, to simultaneously animate the width using the linear
easing office and the summit using the easeOutBounce
easing part:
i 2 3 4 v six 7 8 ix 10 11 12 thirteen 14 15 | |
Every bit previously noted, a plugin is required for the easeOutBounce
function.
Examples:
Click the button to animate the div with a number of different properties.
1 2 3 4 5 6 7 8 nine ten 11 12 13 14 15 16 17 eighteen nineteen 20 21 22 23 24 25 26 27 28 29 thirty 31 32 33 34 35 | |
Demo:
Animates a div's left property with a relative value. Click several times on the buttons to meet the relative animations queued up.
1 two 3 4 5 6 7 8 9 10 11 12 xiii 14 15 sixteen 17 18 19 twenty 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
Demo:
The get-go push shows how an unqueued animation works. It expands the div out to 90% width while the font-size is increasing. Once the font-size change is complete, the border blitheness will brainstorm. The 2d push starts a traditional chained animation, where each animation volition start once the previous blitheness on the element has completed.
1 2 three 4 5 half dozen 7 8 9 ten 11 12 13 xiv 15 16 17 18 19 twenty 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 forty 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
Demo:
Animates the first div'due south left property and synchronizes the remaining divs, using the footstep function to set their left backdrop at each phase of the animation.
1 2 3 4 5 6 vii 8 9 10 11 12 xiii 14 15 sixteen 17 eighteen 19 20 21 22 23 24 25 26 27 28 29 xxx 31 32 33 34 35 36 37 38 39 40 41 42 | |
Demo:
Animate all paragraphs to toggle both summit and opacity, completing the animation within 600 milliseconds.
Breathing all paragraphs to a left manner of 50 and opacity of 1 (opaque, visible), completing the blitheness inside 500 milliseconds.
Animate the left and opacity manner backdrop of all paragraphs; run the animation exterior the queue, so that it volition automatically start without waiting for its turn.
An example of using an 'easing' function to provide a unlike style of animation. This will but work if you take a plugin that provides this easing function. Note, this lawmaking will do nothing unless the paragraph element is hidden.
Animates all paragraphs to toggle both height and opacity, completing the animation within 600 milliseconds.
Use an easing function to provide a unlike style of animation. This will merely work if you have a plugin that provides this easing function.
Animate all paragraphs and execute a callback function when the animation is consummate. The first statement is an object of CSS properties, the second specifies that the blitheness should have 1000 milliseconds to complete, the third states the easing type, and the quaternary argument is an anonymous callback function.
1 2 3 4 five 6 7 | |
Source: https://api.jquery.com/animate/
Posted by: bootsdoner1941.blogspot.com
0 Response to "Should You Animate With Jquery Or Css"
Post a Comment