Search code examples
javascripthtmlcssbutton

Does anyone know how I can keep my drop down menus open when a button is clicked?


I'm in the middle of learning JS so I'm new to it and self-learning. When I click the dropdown buttons my buttons don't stay active and open for half a second and close again. would anybody be able to walk me through how I can keep them open with JS? Here is my code so far! would appreciate the help! I didn't know a project by the way so that's why it's on Elon Musk lol.

let menu_icons = document.querySelector('.menu-icons'),
nav = document.querySelector('nav');

menu_icons.addEventListener('click', () => {
    nav.classList.toggle('active');
});
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}

#fullscreen-video-container {
    position: absolute;
    top: 0;
    left: 0;
    scale: 100%;
    overflow: hidden;
    z-index: -10;
  }
  
  #fullscreen-video-container video {
    object-fit: cover;
    width: 100%;
    height: 100%;
  }

img {
    top: 5px;
    position: relative; 
    height: 200px;
    width: 200px;
    
  }
:root {
    --main-color: rgb(228, 145, 37);
}

html {
    font-size: 10px;
}

body {
   
    background-attachment: fixed;
    background-size: cover;
    background-repeat: no-repeat;
    min-height: 100vh;
    overflow-x: hidden;    
}

body::before {
    position: absolute;
    min-height: 100vh;
    content: '';
    width: 100%;
 
}

a {
    text-decoration: none;
}

ul {
    list-style: none;
}

header {
    width: 100%;
    position: absolute;
    top: 0;
    left: 0;
}

.container {
    width: 100%;
    margin: 0 auto;
}

.menu-icons {
    display: none;
    align-items: center;
    justify-content: center;
    color: #eee;
    font-size: 2rem;
    position: absolute;
    top: 50%;
    right: 2rem;
    transform: translateY(-50%);
    -webkit-transform: translateY(-50%);
    -moz-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    -o-transform: translateY(-50%);
    cursor: pointer;
    z-index: 1500;
    /* display: none; */
}

nav {
    width: 100%;
    border-radius: 1px solid rgba(255, 255, 255, 0.1);
    -webkit-border-radius: 1px solid rgba(255, 255, 255, 0.1);
    -moz-border-radius: 1px solid rgba(255, 255, 255, 0.1);
    -ms-border-radius: 1px solid rgba(255, 255, 255, 0.1);
    -o-border-radius: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    align-items: center;
    height: 60px;
   
    justify-content: center;
    gap: 2rem;
}


.nav-list {
    display: flex;
    align-items: center;
}

.nav-list li {
    line-height: 8rem;
    position: relative
}

.sub-menu li {
    line-height: 4rem;
}

.nav-list a {
    display: block;
    color: #eee;
    padding: 0 1.5rem;
    font-size: 1.4rem;
    text-transform: uppercase;
    transition: color 650ms;
    -webkit-transition: color 650ms;
    -moz-transition: color 650ms;
    -ms-transition: color 650ms;
    -o-transition: color 650ms;
    position: relative;
    width: 100%;
    align-items: center;
    top: 15px;
    margin-left: 50px;
    
}

.nav-list a:active {
    color: var(--main-color);
}

.btn {
    padding: 1.3rem;
    display: inline-block;
    border: 2px solid var(--main-color);
    border-radius: 0.5rem;
    -webkit-border-radius: 0.5rem;
    -moz-border-radius: 0.5rem;
    -ms-border-radius: 0.5rem;
    -o-border-radius: 0.5rem;
    transition: background-color 650ms;
    -webkit-transition: background-color 650ms;
    -moz-transition: background-color 650ms;
    -ms-transition: background-color 650ms;
    -o-transition: background-color 650ms;
}

.btn:active {
    color: var(--main-color);
}

.sub-menu {
    width: 20rem;
    display: block;
    position: absolute;
    border-top: 3px solid var(--main-color);
    background-color: #ff2222;
    z-index: 100;
    top: 16rem;
    transition: all 250ms ease;
    -webkit-transition: all 250ms ease;
    -moz-transition: all 250ms ease;
    -ms-transition: all 250ms ease;
    -o-transition: all 250ms ease;
    opacity: 0;
    visibility: hidden;
}

.sub-menu::before {
    content: '';
    position: absolute;
    top: -2.5rem;
    left: 3rem;
    border: 1.2rem solid transparent;
    border-bottom: 1.2rem solid var(--main-color);
}

.sub-menu .sub-menu::before {
    top: .9rem;
    left: -2.5rem;
    border: 1.2rem solid transparent;
    border-right-color: var(--main-color);
}

.sub-menu .sub-menu {
    border-top: none;
    border-left: 3px solid var(--main-color);
    top: 0;
    left: 160%;
}


.nav-list li:active>.sub-menu {
    top: 8rem;
    opacity: 1;
    visibility: visible;
}

.sub-menu li:active>.sub-menu {
    top: 0;
    left: 100%;
}

li.move-right {
    margin: auto 0 auto auto;
    line-height: initial;
}

@media screen  and (max-width: 900px) {
    .nav-list {
        position: fixed;
        top: 0;
        height: 100vh;
        flex-direction: column;
        background-color: #191919;
        z-index: 1000;
        align-items: initial;
        display: none;
        overflow-y: scroll;
        width: 50%;
        right: 0;
    }

    .nav-list > li {
        line-height: 6rem;
    }

    .sub-menu {
        position: initial;
        border: 3px solid transparent;
        margin-left: 1rem;
        max-height: 0;
        background: rgba(0, 0, 0, 0.5);
        overflow: hidden;
        margin-right: 0.4rem;
        padding-right: 1rem;
    }

    .sub-menu .sub-menu {
        border: none;
        overflow: hidden;
        margin-right: 0.4rem;
    }

    .sub-menu::before {
        display: none;
    }

    .nav-list li:active > .sub-menu {
        opacity: 1;
        visibility: visible;
        max-height: initial;
    }

    li.move-right {
        margin: 0 auto 0 0;
        line-height: initial;
    }

    .menu-icons {
        display: block;
    }

    .fa-times {
        display: none;
    }

    nav.active .fa-times {
        display: block;
    }

    nav.active .fa-bars {
        display: none;
    }
    
    nav.active .nav-list {
        display: flex;
    }
}
<!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="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css">
    <link rel="stylesheet" href="{{ url_for('static', filename='CSS/test.css') }}">
    <title>Document</title>
</head>
<body>
    <header>
        <div class="container">
            <nav>
                <div class="menu-icons">
                   <i class="fas fa-bars"></i>
                   <i class="fas fa-times"></i>
                </div>

                <a href="#" class="logo"><img class="logo" src="{{ url_for('static', filename='Images/logo2.png') }}"></a>

                <ul class="nav-list">
                    <li>
                        <a href="#">Home</a>
                    </li>

                    
                  <li>
                    <a href="#">ELON MUSK</a>
                </li>
                <li>
                  <a href="#">THROUGH TIME</a>
              </li>

                    <li>
                        <a href="#">ELON ODYSSEY <i class="fas fa-caret-down"></i></a>
                        <ul class="sub-menu">
                            <li>
                                <a href="#">SPACE X</a>
                            </li>

                            <li>
                              <a href="#">NEUROLINK</a>
                          </li>
                          <li>
                            <a href="#">TESLA</a>
                          </li>
                          
                        </ul>

                    </li>

                   
                    <li>
                        <a href="#">MORE
                            <i class="fas fa-caret-down"></i>
                        </a>
                        <ul class="sub-menu">
                            <li>
                                <a href="#">DOGE</a>
                            </li>
                            <li>
                              <a href="#">FORUMS</a>
                          </li>
                          <li>
                            <a href="#">BLOGS</a>
                          </li>
                        </ul>
                    </li>
                    
                    
  
                </ul>
                
            </nav>
        </div>
    </header>

    <div id="fullscreen-video-container">
      <video autoplay loop muted>
          <source src="{{ url_for('static', filename='/Videos/Background.mp4') }}">
      </video>
  </div>

      
    <script src="{{ url_for('static', filename='js/test.js') }}"></script>
</body>
</html>

Solution

  • You're pretty close. You need to change .nav-list li:active > .sub-menu to .nav-list li.active > .sub-menu and add some js to toggle the active class when a menu item is clicked:

    let menu_lis = document.querySelectorAll('.nav-list li');
    for (let menu_li of menu_lis) {
      menu_li.addEventListener('click', () => {
        menu_li.classList.toggle('active');
      });
      
    }
    

    Here is a working example snippet.

    let menu_icons = document.querySelector('.menu-icons');
    let nav = document.querySelector('nav');
    
    menu_icons.addEventListener('click', () => {
        nav.classList.toggle('active');
    });
    
    let menu_lis = document.querySelectorAll('.nav-list li');
    for (let menu_li of menu_lis) {
      menu_li.addEventListener('click', () => {
        menu_li.classList.toggle('active');
      });
      
    }
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        font-family: 'Poppins', sans-serif;
    }
    
    #fullscreen-video-container {
        position: absolute;
        top: 0;
        left: 0;
        scale: 100%;
        overflow: hidden;
        z-index: -10;
      }
      
      #fullscreen-video-container video {
        object-fit: cover;
        width: 100%;
        height: 100%;
      }
    
    img {
        top: 5px;
        position: relative; 
        height: 200px;
        width: 200px;
        
      }
    :root {
        --main-color: rgb(228, 145, 37);
    }
    
    html {
        font-size: 10px;
    }
    
    body {
       
        background-attachment: fixed;
        background-size: cover;
        background-repeat: no-repeat;
        min-height: 100vh;
        overflow-x: hidden;    
    }
    
    body::before {
        position: absolute;
        min-height: 100vh;
        content: '';
        width: 100%;
     
    }
    
    a {
        text-decoration: none;
    }
    
    ul {
        list-style: none;
    }
    
    header {
        width: 100%;
        position: absolute;
        top: 0;
        left: 0;
    }
    
    .container {
        width: 100%;
        margin: 0 auto;
    }
    
    .menu-icons {
        display: none;
        align-items: center;
        justify-content: center;
        color: #eee;
        font-size: 2rem;
        position: absolute;
        top: 50%;
        right: 2rem;
        transform: translateY(-50%);
        -webkit-transform: translateY(-50%);
        -moz-transform: translateY(-50%);
        -ms-transform: translateY(-50%);
        -o-transform: translateY(-50%);
        cursor: pointer;
        z-index: 1500;
        /* display: none; */
    }
    
    nav {
        width: 100%;
        border-radius: 1px solid rgba(255, 255, 255, 0.1);
        -webkit-border-radius: 1px solid rgba(255, 255, 255, 0.1);
        -moz-border-radius: 1px solid rgba(255, 255, 255, 0.1);
        -ms-border-radius: 1px solid rgba(255, 255, 255, 0.1);
        -o-border-radius: 1px solid rgba(255, 255, 255, 0.1);
        display: flex;
        align-items: center;
        height: 60px;
       
        justify-content: center;
        gap: 2rem;
    }
    
    
    .nav-list {
        display: flex;
        align-items: center;
    }
    
    .nav-list li {
        line-height: 8rem;
        position: relative
    }
    
    .sub-menu li {
        line-height: 4rem;
    }
    
    .nav-list a {
        display: block;
        color: #eee;
        padding: 0 1.5rem;
        font-size: 1.4rem;
        text-transform: uppercase;
        transition: color 650ms;
        -webkit-transition: color 650ms;
        -moz-transition: color 650ms;
        -ms-transition: color 650ms;
        -o-transition: color 650ms;
        position: relative;
        width: 100%;
        align-items: center;
        top: 15px;
        margin-left: 50px;
        
    }
    
    .nav-list a:active {
        color: var(--main-color);
    }
    
    .btn {
        padding: 1.3rem;
        display: inline-block;
        border: 2px solid var(--main-color);
        border-radius: 0.5rem;
        -webkit-border-radius: 0.5rem;
        -moz-border-radius: 0.5rem;
        -ms-border-radius: 0.5rem;
        -o-border-radius: 0.5rem;
        transition: background-color 650ms;
        -webkit-transition: background-color 650ms;
        -moz-transition: background-color 650ms;
        -ms-transition: background-color 650ms;
        -o-transition: background-color 650ms;
    }
    
    .btn:active {
        color: var(--main-color);
    }
    
    .sub-menu {
        width: 20rem;
        display: block;
        position: absolute;
        border-top: 3px solid var(--main-color);
        background-color: #ff2222;
        z-index: 100;
        top: 16rem;
        transition: all 250ms ease;
        -webkit-transition: all 250ms ease;
        -moz-transition: all 250ms ease;
        -ms-transition: all 250ms ease;
        -o-transition: all 250ms ease;
        opacity: 0;
        visibility: hidden;
    }
    
    .sub-menu::before {
        content: '';
        position: absolute;
        top: -2.5rem;
        left: 3rem;
        border: 1.2rem solid transparent;
        border-bottom: 1.2rem solid var(--main-color);
    }
    
    .sub-menu .sub-menu::before {
        top: .9rem;
        left: -2.5rem;
        border: 1.2rem solid transparent;
        border-right-color: var(--main-color);
    }
    
    .sub-menu .sub-menu {
        border-top: none;
        border-left: 3px solid var(--main-color);
        top: 0;
        left: 160%;
    }
    
    
    .nav-list li:active>.sub-menu {
        top: 8rem;
        opacity: 1;
        visibility: visible;
    }
    
    .sub-menu li:active>.sub-menu {
        top: 0;
        left: 100%;
    }
    
    li.move-right {
        margin: auto 0 auto auto;
        line-height: initial;
    }
    
    @media screen  and (max-width: 900px) {
        .nav-list {
            position: fixed;
            top: 0;
            height: 100vh;
            flex-direction: column;
            background-color: #191919;
            z-index: 1000;
            align-items: initial;
            display: none;
            overflow-y: scroll;
            width: 50%;
            right: 0;
        }
    
        .nav-list > li {
            line-height: 6rem;
        }
    
        .sub-menu {
            position: initial;
            border: 3px solid transparent;
            margin-left: 1rem;
            max-height: 0;
            background: rgba(0, 0, 0, 0.5);
            overflow: hidden;
            margin-right: 0.4rem;
            padding-right: 1rem;
        }
    
        .sub-menu .sub-menu {
            border: none;
            overflow: hidden;
            margin-right: 0.4rem;
        }
    
        .sub-menu::before {
            display: none;
        }
    
        .nav-list li.active > .sub-menu {
            opacity: 1;
            visibility: visible;
            max-height: initial;
        }
    
        li.move-right {
            margin: 0 auto 0 0;
            line-height: initial;
        }
    
        .menu-icons {
            display: block;
        }
    
        .fa-times {
            display: none;
        }
    
        nav.active .fa-times {
            display: block;
        }
    
        nav.active .fa-bars {
            display: none;
        }
        
        nav.active .nav-list {
            display: flex;
        }
    }
    <!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="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css">
        <link rel="stylesheet" href="{{ url_for('static', filename='CSS/test.css') }}">
        <title>Document</title>
    </head>
    <body>
        <header>
            <div class="container">
                <nav>
                    <div class="menu-icons">
                       <i class="fas fa-bars"></i>
                       <i class="fas fa-times"></i>
                    </div>
    
                    <a href="#" class="logo"><img class="logo" src="{{ url_for('static', filename='Images/logo2.png') }}"></a>
    
                    <ul class="nav-list">
                        <li>
                            <a href="#">Home</a>
                        </li>
    
                        
                      <li>
                        <a href="#">ELON MUSK</a>
                    </li>
                    <li>
                      <a href="#">THROUGH TIME</a>
                  </li>
    
                        <li>
                            <a href="#">ELON ODYSSEY <i class="fas fa-caret-down"></i></a>
                            <ul class="sub-menu">
                                <li>
                                    <a href="#">SPACE X</a>
                                </li>
    
                                <li>
                                  <a href="#">NEUROLINK</a>
                              </li>
                              <li>
                                <a href="#">TESLA</a>
                              </li>
                              
                            </ul>
    
                        </li>
    
                       
                        <li>
                            <a href="#">MORE
                                <i class="fas fa-caret-down"></i>
                            </a>
                            <ul class="sub-menu">
                                <li>
                                    <a href="#">DOGE</a>
                                </li>
                                <li>
                                  <a href="#">FORUMS</a>
                              </li>
                              <li>
                                <a href="#">BLOGS</a>
                              </li>
                            </ul>
                        </li>
                        
                        
      
                    </ul>
                    
                </nav>
            </div>
        </header>
    
        <div id="fullscreen-video-container">
          <video autoplay loop muted>
              <source src="{{ url_for('static', filename='/Videos/Background.mp4') }}">
          </video>
      </div>
    
          
        <script src="{{ url_for('static', filename='js/test.js') }}"></script>
    </body>
    </html>