Home > other >  responsive nav bar add class to divelement
responsive nav bar add class to divelement

Time:04-29

i took this code from w3school where create a responsive nav bar. the nav bar add icon, add functionality for icon and change menu display. in js code, nav is assign to div element and nav has 2 case, if has class topnav or else (dont have class topnav). in html, nav has class top nav, so i dont understand what else case is for. else case is needed because when i remove else case functionality also change. please someone explain

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #333;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

/* .topnav a.active {
  background-color: #04aa6d;
  color: white;
} */

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  /* only display home */
  .topnav a:not(:first-child) {
    display: none;
  }
  /* display icon dropdown  */
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {
    position: relative;
  }
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"
    />
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div  id="myTopnav">
      <a href="#home">Home</a>
      <a href="#news">News</a>
      <a href="#contact">Contact</a>
      <a href="#about">About</a>
      <a href="javascript:void(0);"  onclick="myFunction()">
        <i ></i>
      </a>
    </div>

    <script>
      function myFunction() {
        //objecthtmldivelement
        var nav = document.getElementById("myTopnav");
        // alert(nav);
        if (nav.className === "topnav") {
          //class responsive position icon to top right
          //and display a as block text float left
          nav.className  = " responsive";
        } else {
          nav.className = "topnav";
        }
      }
    </script>
  </body>
</html>

CodePudding user response:

this function is for /* Toggle between adding and removing the "responsive" class to topnav when the user clicks on the icon */

else part is for removing the "responsive" class so that it remain topnav like default it was

CodePudding user response:

nav.className === "topnav" is a exact match statement, so if you click the icon, the nav.className become 'topnav responsive', which is not exact match to 'topnav'(in other words, 'topnav' === 'topnav responsive' is false), this is the functionality that the else block want to handle.

  • Related