w3resource

jQuery: Animate the hiding and showing of two divs, delaying the first before showing it

jQuery Fundamental - II : Exercise-74

Animate the hiding and showing of two divs, delaying the first before showing it.

Sample solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Animate the hiding and showing of two divs, delaying the first before showing it.</title>
</head>
<body>
<p><button>Run</button></p>
<div class="div1"></div>
<div class="div2"></div>
</body>
</html>

CSS Code :

 div {
 position: absolute;
 width: 50px;
 height: 50px;
 float: left;
 }
 .div1 {
 background-color: red;
 left: 0;
 }
 .div2 {
 background-color: green;
 left: 80px;
 }
 

JavaScript Code :

$( "button" ).click(function() {
$( "div.div1" ).slideUp( 300 ).delay( 800 ).fadeIn( 400 );
$( "div.div2" ).slideUp( 400 ).fadeIn( 300 );
});

See the Pen jquery-fundamental-exercise-74 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Resolve a deferred object when the user clicks a button, triggering a number of callback functions.
Next: Click on a paragraph and add another paragraph.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.