Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

jQuery animate()


The jQuery animate() method provides you a way to create custom animations.

Syntax:

  1. $(selector).animate({params}, speed, callback);

Here, params parameter defines the CSS properties to be animated.

The speed parameter is optional and specifies the duration of the effect. It can be set as “slow” , “fast” or milliseconds.

The callback parameter is also optional and it is a function which is executed after the animation completes.

Let’s take a simple example to see the animation effect.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src=“http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>
  5. <script>
  6. $(document).ready(function(){
  7.     $(“button”).click(function(){
  8.         $(“div”).animate({left: ‘450px’});
  9.     });
  10. });
  11. </script>
  12. </head>
  13. <body>
  14. <button>Start Animation</button>
  15. <p>A simple animation example:</p>
  16. <div style=“background:#98bf21;height:100px;width:100px;position:absolute;”></div>
  17. </body>
  18. </html>

Test it Now

Output:

A simple animation example:

Note: The default position of all HTML elements is static. If you want to manipulate their position, set the CSS position property to the element to relative, fixed or absolute.

jQuery animate() method using multiple properties

You can use multiple properties to animate at the same time.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src=“http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>
  5. <script>
  6. $(document).ready(function(){
  7.     $(“button”).click(function(){
  8.         $(“div”).animate({
  9.             left: ‘250px’,
  10.             opacity: ‘0.5’,
  11.             height: ‘150px’,
  12.             width: ‘150px’
  13.         });
  14.     });
  15. });
  16. </script>
  17. </head>
  18. <body>
  19. <button>Start Animation</button>
  20. <div style=“background:#125f21;height:100px;width:100px;position:absolute;”></div>
  21. </body>
  22. </html>

Test it Now

Output:

jQuery animate() method using relative values

You can also define relative values (it is relative to the element’s current value) by putting += or -= in front of the value.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src=“http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>
  5. <script>
  6. $(document).ready(function(){
  7.     $(“button”).click(function(){
  8.         $(“div”).animate({
  9.             left: ‘250px’,
  10.             height: ‘+=150px’,
  11.             width: ‘+=150px’
  12.         });
  13.     });
  14. });
  15. </script>
  16. </head>
  17. <body>
  18. <button>Start Animation</button>
  19. <div style=“background:#98bf21;height:100px;width:100px;position:absolute;”></div>
  20. </body>
  21. </html>

Test it Now

Output:

jQuery animate() method using predefined value

You can also specify a property’s animation value as “show” , “hide” , or “toggle”.

In this example, we are using “toggle” value for height, it means it will show/hide the selected element.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src=“http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>
  5. <script>
  6. $(document).ready(function(){
  7.     $(“button”).click(function(){
  8.         $(“div”).animate({
  9.             height: ‘toggle’
  10.         });
  11.     });
  12. });
  13. </script>
  14. </head>
  15. <body>
  16. <button>Start Animation</button>
  17. <div style=“background:#98bf21;height:100px;width:100px;position:absolute;”></div>
  18. </body>
  19. </html>

Test it Now

Output:

jQuery Color animation

You can also animate the properties of elements between colors.

  1. <!doctype html>
  2. <html lang=“en”>
  3. <head>
  4.   <meta charset=“utf-8”>
  5.   <title>jQuery UI Effects – Animate demo</title>
  6.   <link rel=“stylesheet” href=“http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css”>
  7.   <script src=“http://code.jquery.com/jquery-1.10.2.js”></script>
  8.   <script src=“http://code.jquery.com/ui/1.11.4/jquery-ui.js”></script>
  9.   <style>
  10.     .toggler { width: 500px; height: 200px; position: relative; }
  11.     #button { padding: .5em 1em; text-decoration: none; }
  12.     #effect { width: 240px; height: 135px; padding: 0.4em; position: relative; background: #fff; }
  13.     #effect h3 { margin: 0; padding: 0.4em; text-align: center; }
  14.   </style>
  15.   <script>
  16.   $(function() {
  17.     var state = true;
  18.     $( “#button” ).click(function() {
  19.       if ( state ) {
  20.         $( “#effect” ).animate({
  21.           backgroundColor: “#aa0000”,
  22.           color: “#fff”,
  23.           width: 500
  24.         }, 1000 );
  25.       } else {
  26.         $( “#effect” ).animate({
  27.           backgroundColor: “#fff”,
  28.           color: “#000”,
  29.           width: 240
  30.         }, 1000 );
  31.       }
  32.       state = !state;
  33.     });
  34.   });
  35.   </script>
  36. </head>
  37. <body>
  38. <div class=“toggler”>
  39.   <div id=“effect” class=“ui-widget-content ui-corner-all”>
  40.     <h3 class=“ui-widget-header ui-corner-all”>Animate</h3>
  41.     <p>Kreationnext.com is the best tutorial website to learn Java and other programming languages.</p>
  42.   </div>
  43. </div>
  44.  <button id=“button” class=“ui-state-default ui-corner-all”>Toggle Effect</button>
  45. </body>
  46. </html>

Test it Now

Comments

comments