w3resource

jQuery Effect: Fade in and fade out all division elements

jQuery Effect : Exercise-5 with Solution

Fade in and fade out all division elements.
Sample Data :

HTML code:

<body>
 <div style="background:#2E9AFE;width:100%;">My Effect is fadeOut Effect</div>
 <button id="btn2">Fade In (3 Second)</button>
 <button id="btn1">Fade Out (3 Second)</button>
 </body>

Solution:

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <title>Fade in and fade out all division elements</title>
</head>
<body>
<div style="background:#2E9AFE;width:100%;">My Effect is fadeOut Effect</div>
<button id="btn2">Fade In (3 Second)</button>
<button id="btn1">Fade Out (3 Second)</button>
</body>
</html>

JavaScript Code :

$(document).ready(function(){
  $("#btn1").click(function(){
    $("div").fadeOut(3000);
  });
   $("#btn2").click(function(){
    $("div").fadeIn(3000);
  }); 
});

Note :
In jQuery fadeIn( [duration ] [, complete ] ) method is used to display the matched elements by fading them to opaque. It has the following parameters :

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

In jQuery fadeout( [duration ] [, complete ] ) method is used to hide the matched elements by fading them to transparent. It has the following parameters :

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

Live Demo:

See the Pen jquery-effect-exercise-5 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Use dequeue to end a custom queue function which allows the queue to keep going.
Next: Animates a paragraph to fade to an specified opacity (complete the animation within 500 milliseconds).

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.