Home > Blockchain >  Toggle sidemenu in html
Toggle sidemenu in html

Time:10-17

I have Created a html webpage which has a side menu.

Now I want to close and open the side menu using an onclick function.

How can I create a javascript function so that onclick I must hide the side menu and open it accordingly.

window.addEventListener("DOMContentLoaded", () => {
  document
    .getElementById(".siderbar .toggle-btn")
    .addEventListener("click", function (e) {
      document.getElementById(".sidebar").classList.toggle(".active");
    });
});
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: "Raleway", sans-serif;
}

.sidebar {
  position: fixed;
  top: 0;
  left: -250px;
  width: 250px;
  height: 100vh;
  background-color: #0170c1;
  transition: all 300ms ease-in-out;
}

/* sidebar hamburger togglebutton*/

.sidebar .toggle-btn {
  position: absolute;
  top: 10px;
  right: -60px;
  width: 40px;
  height: 40px;
  cursor: pointer;
}

.sidebar .toggle-btn span {
  position: absolute;
  width: 100%;
  height: 4px;
  background-color: black;
  transform: translateY(-50%);
  transition: all 300ms ease-in-out;
}

.sidebar .toggle-btn span:nth-child(1) {
  top: 10%;
}

.sidebar .toggle-btn span:nth-child(2) {
  top: 50%;
}

.sidebar .toggle-btn span:nth-child(3) {
  top: 90%;
}

/* sidebar items*/

.links {
  margin-top: 150px;
}

.sidebar .links a {
  display: block;
  padding: 15px 10px;
  text-decoration: none;
  color: #f5f5f5;
  cursor: pointer;
  border-bottom: 1px solid #1b1b1b;
}

.sidebar .links a.active,
.sidebar .links a:hover {
  background-color: white;
  color: #0170c1;
}

.sidebar .links a i {
  display: inline-block;
  margin-right: 10px;
}

/* sidebar toggle with button*/

.sidebar.active {
  left: 0px;
}

.sidebar.active .toggle-btn span:nth-child(1) {
  top: 50%;
  transform: translateY(-50%) rotate(45deg);
}
.sidebar.active .toggle-btn span:nth-child(2) {
  display: none;
}
.sidebar.active .toggle-btn span:nth-child(3) {
  top: 50%;
  transform: translateY(-50%) rotate(-45deg);
}
<!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" />
    <title>Dashboard</title>

    <link rel="stylesheet" href="index.css" />

    <link
      rel="stylesheet"
      href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css"
    />

    <script src="script.js"></script>
  </head>
  <body>
    <div >
      <div ></div>

      <div >
        <div >
          <span></span>
          <span></span>
          <span></span>
        </div>
        <div >
          <a href="#">
            <i ></i>
            Home
          </a>
          <a href="#">
            <i ></i>
            List of Companies
          </a>
          <a href="#">
            <i ></i>
            Line of Business
          </a>
          <a href="#">
            <i ></i>
            Application Features
          </a>
          <a href="#">
            <i ></i>
            Upload Settings
          </a>
        </div>
      </div>
    </div>
  </body>
</html>

CodePudding user response:

You missusing the getElementById, you need to use querySelector instead.

const toggleButton = document.querySelector(".sidebar .toggle-btn");

toggleButton.addEventListener("click", function (e) {
  const sidebar = document.querySelector(".sidebar");
  sidebar.classList.toggle("active");
});
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: "Raleway", sans-serif;
}

.sidebar {
  position: fixed;
  top: 0;
  left: -250px;
  width: 250px;
  height: 100vh;
  background-color: #0170c1;
  transition: all 300ms ease-in-out;
}

/* sidebar hamburger togglebutton*/

.sidebar .toggle-btn {
  position: absolute;
  top: 10px;
  right: -60px;
  width: 40px;
  height: 40px;
  cursor: pointer;
}

.sidebar .toggle-btn span {
  position: absolute;
  width: 100%;
  height: 4px;
  background-color: black;
  transform: translateY(-50%);
  transition: all 300ms ease-in-out;
}

.sidebar .toggle-btn span:nth-child(1) {
  top: 10%;
}

.sidebar .toggle-btn span:nth-child(2) {
  top: 50%;
}

.sidebar .toggle-btn span:nth-child(3) {
  top: 90%;
}

/* sidebar items*/

.links {
  margin-top: 150px;
}

.sidebar .links a {
  display: block;
  padding: 15px 10px;
  text-decoration: none;
  color: #f5f5f5;
  cursor: pointer;
  border-bottom: 1px solid #1b1b1b;
}

.sidebar .links a.active,
.sidebar .links a:hover {
  background-color: white;
  color: #0170c1;
}

.sidebar .links a i {
  display: inline-block;
  margin-right: 10px;
}

/* sidebar toggle with button*/

.sidebar.active {
  left: 0px;
}

.sidebar.active .toggle-btn span:nth-child(1) {
  top: 50%;
  transform: translateY(-50%) rotate(45deg);
}
.sidebar.active .toggle-btn span:nth-child(2) {
  display: none;
}
.sidebar.active .toggle-btn span:nth-child(3) {
  top: 50%;
  transform: translateY(-50%) rotate(-45deg);
}
<!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" />
    <title>Dashboard</title>

    <link rel="stylesheet" href="index.css" />

    <link
      rel="stylesheet"
      href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css"
    />
  </head>
  <body>
    <div >
      <div ></div>

      <div >
        <div >
          <span></span>
          <span></span>
          <span></span>
        </div>
        <div >
          <a href="#">
            <i ></i>
            Home
          </a>
          <a href="#">
            <i ></i>
            List of Companies
          </a>
          <a href="#">
            <i ></i>
            Line of Business
          </a>
          <a href="#">
            <i ></i>
            Application Features
          </a>
          <a href="#">
            <i ></i>
            Upload Settings
          </a>
        </div>
      </div>
    </div>
    <script src="script.js"></script>
  </body>
</html>

  • Related