w3resource

jQuery Effect: Animates all divs to slide down and slide up

jQuery Effect : Exercise-14 with Solution

Animates all divs to slide down and slide up.
Sample Data :

HTML code:

<body>
 <button class="btn1">Slide down</button>
 <button class="btn2">Slide up</button>
 <br><br>
  <div></div>
  <div></div>
  <div></div>
</body>

Solution:

HTML Code :


<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <style>
  div {
    background: #0694EC;
    margin: 3px;
    width: 80px;
    height: 100px;
    display: none;
    float: left;
  }
  </style>
  <meta charset="utf-8">
  <title>Animates all divs to slide down and slide up</title>
</head>
<body>
  <button class="btn1">Slide down</button>
  <button class="btn2">Slide up</button>
<br><br>
  <div></div>
<div></div>
<div></div>
</body>
</html>

JavaScript Code :


$(".btn1").click(function(){
        $("div").slideDown();
    });  
$(".btn2").click(function(){
        $("div").slideUp();
    });

Note :

In jQuery slideDown( [duration ] [, complete ] ) method is used to display the matched elements with a sliding motion. slideUp( [duration ] [, complete ] ) method is used to hide the matched elements with a sliding motion. Both the methods have same parameters :

  • duration : A string or number determining how long the animation will run. [Type- Number or String]
  • complete : A function to call once the animation is complete. [Type: Function()]

Live Demo:

See the Pen jquery-events-exercise-1 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Animates all hidden elements to show slowly.
Next: jQuery Exercises

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/jquery-exercises/jquery-effect-exercise-14.php