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

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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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>

    chevron_right

    
    

  • 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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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>

    chevron_right

    
    

  • Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.