Expain the fade toggle method in JQuery with example.

Asked 28-Jul-2021
Viewed 405 times

1 Answer


1

jQuery fadeToggle()
 jQuery's fadeToggle() method is used for fadeIn() and fadeOut(). If the element is visible for some time then it will be hidden and if it is hidden for some time then it will show
Syntax:

$(selector).fadeToggle();
$(selector).fadeToggle(speed,callback);
Speed and callback is an optional parameter. Speed specifies the delay of showing elements these possible vales are 'slow', 'fast' and milliseconds. Callback function to be called after completion of fadeIn() or fadeOut(). We also put true and false value for showing and hiding element respectively.
<!DOCTYPE html>

<html>
<head>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js'></script>
<script>
    $(document).ready(function () {
        $('button').click(function () {
            $('#div1').fadeToggle();
            $('#div2').fadeToggle('slow');
            $('#div3').fadeToggle(2000);
            $(this).text() == 'Click to fadeIn' ? $(this).text('Click to fadeOut') : $(this).text('Click to fadeIn') ;
        });
    });
</script>
</head>
<body>
<button>Click to fadeIn</button><br><br>
<div id='div1' style='width:80px;height:80px;background-color:pink;'></div>
<br>
<div id='div2' style='width:80px;height:80px;background-color:red;'></div>
<br>
<div id='div3' style='width:80px;height:80px;background-color:black;'></div>
</body>
</html>
Expain the fade toggle method in JQuery with example