Open In App

How to perform fadeOut() and slideUp() together in JavaScript ?

Improve
Improve
Like Article
Like
Save
Share
Report

Given a document, The task is to perform the fadeOut() and slideUP() at the same time with the help of JavaScript. Here 2 approaches are discussed.

Approach 1:
First select the element. Use animate() method to perform the both operations, this method takes 4 arguments but in this case only 2 are used. First is the CSS that we want to animate and the second is speed. CSS is { height: 0, opacity: 0} and speed used is ‘slow’. Height property is used to perform the slideUp effect and opacity is used to perform the fadeOut effect.

  • Example: This example implements the above approach.




    <!DOCTYPE HTML>
    <html>
      
    <head>
        <title>
            Perform fadeOut() and slideUp()
          at the same time with JavaScript
        </title>
        <style>
            #div {
                background: green;
                height: 100px;
                width: 200px;
                margin: 0 auto;
                color: white;
            }
        </style>
        <script src=
        </script>
    </head>
      
    <body style="text-align:center;">
        <h1 style="color:green;">  
                GeeksForGeeks  
            </h1>
        <p id="GFG_UP" 
           style="font-size: 19px; font-weight: bold;">
        </p>
        <div id="div">
            This is Div box.
        </div>
        <br>
        <button onClick="GFG_Fun()">
            click here
        </button>
        <br>
        <p id="GFG_DOWN" 
           style="color: green; 
                  font-size: 24px; 
                  font-weight: bold;">
        </p>
        <script>
            $('#GFG_UP').text(
              "Click on button to perform fadeOut and slideUp at same time.");
      
            function GFG_Fun() {
                $("#div").animate({
                    height: 0,
                    opacity: 0
                }, 'slow');
                $('#GFG_DOWN').text(
                  "Both operations performed simultaneously.");
            }
        </script>
    </body>
      
    </html>

    
    

  • Output:

Approach 2:
First select the element. Use animate() method to perform the both operations, this method takes 4 arguments but in this case only 2 are used. First is the CSS that we want to animate and the second is speed. CSS is { height: ‘toggle’, opacity: ‘toggle’} and speed used is ‘slow’. Height property is used to perform the slideUp effect and opacity is used to perform the fadeOut effect. This example can reverse the effect as well.

  • Example: This example implements the above approach.




    <!DOCTYPE HTML>
    <html>
      
    <head>
        <title>
            Perform fadeOut() and slideUp() 
          at the same time with JavaScript
        </title>
        <style>
            #div {
                background: green;
                height: 100px;
                width: 200px;
                margin: 0 auto;
                color: white;
            }
        </style>
        <script src=
        </script>
    </head>
      
    <body style="text-align:center;">
        <h1 style="color:green;">  
                GeeksForGeeks  
            </h1>
        <p id="GFG_UP" 
           style="font-size: 19px;
                  font-weight: bold;">
        </p>
        <div id="div">
            This is Div box.
        </div>
        <br>
        <button onClick="GFG_Fun()">
            click here
        </button>
        <br>
        <p id="GFG_DOWN" 
           style="color: green; 
                  font-size: 24px; 
                  font-weight: bold;">
        </p>
        <script>
            $('#GFG_UP').text(
           "Click on button to perform fadeOut and slideUp at same time.");
      
            function GFG_Fun() {
                $("#div").animate({
                    height: 'toggle',
                    opacity: 'toggle'
                }, 'slow');
                $('#GFG_DOWN').text(
               "Both operations performed simultaneously.");
            }
        </script>
    </body>
      
    </html>

    
    

  • Output:


Last Updated : 30 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads