All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

jQuery Toggle Animation

Last Updated : Mar 11, 2024

jQuery Toggle Animation

In this article we will show you the solution of jQuery toggle animation, here we needs to use slideToggle() and click() methods for achieve the result.

The slideToggle() method animates the height of the matched elements. This causes lower parts or the page to slide up or down, appearing to reveal or conceal the items.

The click() method triggers click event when particular element is manually clicked.

Step By Step Guide On jQuery Toggle Animation :-

Here we defined h1 element and button ‘Toggle’ for generates animation.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and we waiting for click() method’s click event occurrence which is added on button by using tag name ‘button’.

Within this we specifying h1 element then which is appended with slideToggle() method.

<!DOCTYPE html>
<html>
    <head>
        <title>TOGGLE ANIMATION</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $('button').click(function(){
                    $("h1").slideToggle();
                });
            })
        </script>
        </head>
        <body>
            <h1>This is heading tag.</h1>
            <button>Toggle</button>
        </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() method within that we added click method with button element by specifying tag name ‘button’.
  8. Within function we needs to append ‘slideToggle()’ method with ‘h1’ element. As we know this method will automatically does slide up or slide down animation on specified element.
  9. In html block we defined h1 tag with string ‘This is heading tag.’ for shows slide toggle animation and button ‘Toggle’.
  10. If you want to change h1 element text or define any other element instead h1, you just modify this accordingly.
  11. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to create toggle animation using jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.

When we executes program on browser we can see h1 string ‘This is heading tag.’ and button ‘Toggle’ user needs to clicks on it then we can toggle animation of sliding up and down of h1 element.

I hope this article on jQuery toggle animation helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪