Toggle Menu Icon Using JavaScript | Menu Toggle Icon

Hello everyone, you can learn how to create a Toggle Menu Icon Using JavaScript in this blog. So if you want to learn then follow all the steps carefully.  

Source Code Of Toggle Menu Icon

Step 1: Create Index.html File

Create one file with the name index.html and paste the below code in this file. 
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <title>Menu Toggle Icon</title>
</head>
<body>

    <div class="toggle"><i class="fa fa-bars"></i></div>

    <script>

        let btn = document.querySelector(".toggle");
        let icon = btn.querySelector(".fa-bars");

        btn.onclick = function (){
            if(icon.classList.contains("fa-bars")){
                icon.classList.replace("fa-bars", "fa-times");
            }
            else{
                icon.classList.replace("fa-times", "fa-bars");
            }
        }
    </script>
    
</body>
</html>

 

Step 2: Create style.css File

Create one file with the name style.css and paste the below code in this file.

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body{
    width: 100%;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #1e6091;
}

.toggle{
    background: #ffd60a;
    padding: 10px 15px;
    cursor: pointer;
}

.toggle i{
    font-size: 40px;
    transition: 0.2s;
}

That’s It. if you face any error/problem then comment below or Download this code file and then try again. This file is a Zip file so after download you have to extract it.


For Download Code You have to wait 30 seconds.



Leave a Comment