Home >>jQuery Tutorial >jQuery fadeToggle() Method

jQuery fadeToggle() Method

jQuery fadeToggle() Method

jQuery fadeToggle() method in jQuery is used to toggle between the fadeIn() and fadeOut() methods. fadeToggle() will fade out. If elements are faded in. fadeToggle() will fade in, If elements are faded out.

Syntax:
$(selector).fadeToggle(speed,easing,callback)

Parameter Values

Parameter Description
speed It is an optional parameter and is used to Specify the speed of the fading effect. Its default value is 400 milliseconds

Possible values:

  • milliseconds
  • "slow"
  • "fast"
easing It is an optional parameter and is Specify the speed of the element in points of the animation. Its Default value is "swing"

Possible values:

  • "swing" - faster in the middle, moves slower at the beginning
  • "linear" – it moves in a constant speed
callback It is an optional parameter. After the fadeToggle() method is completed a function to be executed
Here is an Example of jQuery fadeToggle() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeToggle();
$("#div2").fadeToggle("slow");
$("#div3").fadeToggle(3000);
});
});
</script>
</head>
<body>
<button>Click me</button><br><br>
<div id="div1" style="width:80px;height:80px;background-color:orange;"></div>
<br>
<div id="div2" style="width:80px;height:80px;background-color:green;"></div>
<br>
<div id="div3" style="width:80px;height:80px;background-color:red;"></div>
</body>
</html>

Output:




Example 2:

<html> 
<head> 
<style> 
#Outer { 
border: 1px solid black; 
padding-top: 40px; 
height: 140px; 
background: orange; 
display: none; 
} 
</style> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> 
</head> 
<body> 
<div id= "Outer"> 
<h1 style = "color:white;" > 
PHPTPOINT 
</h1> 
</div><br> 
<button id = "btn"> 
Fade Toggle 
</button> 
<script> 
$(document).ready(function() { 
$("#btn").click(function() { 
$("#Outer").fadeToggle(1000); 
}); 
}); 
</script> 
</body> 
</html>

Output:

PHPTPOINT