What is the difference between jQuery.animate() and jQuery.hide()?


jQuery animate()

The animate( ) method performs a custom animation of a set of CSS properties.

Here is the description of all the parameters used by this method −

  • params − A map of CSS properties that the animation will move toward.
  • duration − This is optional parameter representing how long the animation will run.
  • easing − This is optional parameter representing which easing function to use for the transition.
  • callback − This is optional parameter representing a function to call once the animation is complete.

You can try to run the following code to learn how to work with jQuery animate() method:

Example

 Live Demo

<html>

<head>
   <title>jQuery Example</title>
      <script type = "text/javascript"
      src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

      <script type = "text/javascript" language = "javascript">

      $(document).ready(function() {

         $("#out").click(function(){
            $("#block").animate({
               width: "70%",
               opacity: 0.4,
               marginLeft: "0.6in",
               fontSize: "3em",
               borderWidth: "10px"
            }, 1500 );
         });

         $("#in").click(function(){
            $("#block").animate({
            width: "100",
            opacity: 1.0,
            marginLeft: "0in",
            fontSize: "100%",
            borderWidth: "1px"
         }, 1500 );
      });
   });
   </script>

<style>
div {background-color:#bca; width:100px; border:1px solid blue;}
</style>
</head>

<body>
<p>Click on any of the buttons</p>

   <button id = "out"> Animate Out </button>
   <button id = "in"> Animate In</button>

<div id = "block">Hello</div>

</body>
</html>

jQuery hide()

The hide( speed, [callback] ) method hides all matched elements using a graceful animation and firing an optional callback after completion.

Here is the description of all the parameters used by this method −

  • speed − A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
  • callback − This is optional parameter representing a function to call once the animation is complete.

You can try to run the following code to learn how to work with hide() method in jQuery −

Example

 Live Demo

<html>
<head>
<title>The jQuery Example</title>
   <script type = "text/javascript"
   src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

      <script type = "text/javascript" language = "javascript">

      $(document).ready(function() {

         $("#show").click(function () {
            $(".mydiv").show( 200 );
         });

         $("#hide").click(function () {
            $(".mydiv").hide( 200 );
         });

      });

   </script>

<style>
.mydiv{ margin:10px;padding:12px; border:2px solid #666; width:100px; height:100px;}
</style>
</head>
<body>
<div class = "mydiv">
This is a SQUARE.
</div>

   <input id = "hide" type = "button" value = "Hide" />
   <input id = "show" type = "button" value = "Show" />
</body>
</html>

Updated on: 22-Jun-2020

198 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements