Home > Article > Web Front-end > How to use animate to customize animation in jQuery
Animation animate()
01. Simple use of animate() method
Some complex animations cannot be realized through several animation functions learned before, and this time it is powerful animate method.
Operate an element to perform a 3-second fade-in animation, and compare the differences between the two sets of animation settings.
$(elem).fadeOut(3000) $(elem).animate({ opacity:0 },3000)
Obviously, the animate method is more flexible and can accurately control the style attributes to perform animation.
Syntax:
1 .animate( properties [, duration ] [, easing ] [, complete ] )
2 .animate( properties, options )
The .animate() method allows us to create animations on any numeric CSS property. The two syntaxes are almost the same. The only necessary attribute is a set of CSS attribute key-value pairs. This set of properties is similar to the property key-value pairs used to set the .css() method, except that the property scope is more restricted. As for the second parameter, multiple actual parameters can be passed individually or combined into one object.
Parameter decomposition:
properties: an Object object composed of key-value pairs of one or more css properties. It is important to note that all properties used for animation must be numeric unless otherwise stated; these properties will not be able to use basic jQuery functionality if they are not numeric. For example, the common ones, border, margin, padding, width, height, font, left, top, right, bottom, wordSpacing, etc., can all produce animation effects. Background-color is obviously not possible, because the parameter is a value such as red or GBG, which is very useful for plug-ins, otherwise the animation effect cannot be achieved under normal circumstances. Note that CSS styles are set using DOM names (such as "fontSize"), not CSS names (such as "font-size").
Pay special attention to the unit, the unit of the attribute value is pixels (px), unless otherwise stated. The units em and % need to be specified using
.animate({ left: , width: 'px' opacity: 'show', fontSize: "em", }, );
In addition to defining values, each attribute can use 'show', 'hide', and 'toggle'. These shortcuts allow custom hide and show animations to control the display or hiding of elements
.animate({ width: "toggle" });
##If a value starting with += or -= is provided, then The target value is calculated by adding or subtracting the given number from the current value of this attribute
.animate({ left: '+50px' }, "slow");duration: timeAnimation The execution time and duration are in milliseconds; the larger the value, the slower the animation execution is, not the faster it is. It is also possible to provide 'fast' and 'slow' strings, indicating durations of 200 and 600 milliseconds respectively.
Algorithm for easing animation movement:
easing - Specify the easing function to be used, which transition to use Easing function
step: stipulates the function to be executed after each step of each animation is completed
progress: This callback will be executed every time the animation is called, which is a concept of progress
complete: callback when the animation is completed
$('#elem').animate({ width: 'toggle', height: 'toggle' }, { duration: , specialEasing: { width: 'linear', height: 'easeOutBounce' }, complete: function() { $(this).after('<div>Animation complete.</div>'); } });Call the animate() method to create a custom animation effect. Its calling format is: $(selector).animate({params},speed,[callback ])
<body> <h>制作简单的动画效果</h> <img src="images/.png" alt=""/> <div id="tip"></div> <script type="text/javascript"> $(function() { $('img').animate({ width: 'px'; height:'px' }, , function() { $("#tip").html('执行完成!'); }); }) </script> </body>while browsing The effect displayed in the device: As can be seen from the picture, the animate() method is called to display the picture elements with a gradually enlarging animation effect. When the animation is completed, Use the callback function to display the words "Execution Completed!" in the