Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

css - Why my transition on my hamburger menu not working?

I have problem with a transition.

I would like to the transition start after clicking on the icon(bars) on mobile view. But transition doesn't work.When I click on the icon, nav will appear, but transition is not execute.

My code: https://codepen.io/ji-nov-ek/pen/gOwNZea

// Hamburger menu
let toggle = document.getElementById("toggle");
let nav = document.querySelector("nav");

toggle.addEventListener('click', function() {
  if (nav.style.display === "flex") {
    nav.style.display = "none";
    nav.style.opacity = "0";
  } else {
    nav.style.display = "flex";
    nav.style.opacity = "1";
  }
});
/* Global Styles */

@import url("https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap");
* {
  margin: 0;
  padding: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-decoration: none;
  font-family: 'Poppins', sans-serif;
}

html {
  font-size: 10px;
}

ul {
  list-style: none;
}

a {
  text-decoration: none;
  color: #434343;
  font-family: 'Poppins', sans-serif;
}

h1,
h2,
h3 {
  font-family: 'Poppins', sans-serif;
}

body {
  background-color: #ffffff;
}

.case {
  height: 100vh;
}

.case .hero {
  background-color: #ffffff;
}

.case .hero header {
  background-color: #4281C1;
  min-height: 10rem;
  width: 100%;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}

.case .hero header .logo {
  width: 28rem;
}

.case .hero header .logo img {
  width: 100%;
  height: 100%;
}

.case .hero header nav {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
  -ms-flex-direction: row;
  flex-direction: row;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  min-height: 10rem;
  margin: 1rem 0;
}

.case .hero header nav ul {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
  -ms-flex-direction: row;
  flex-direction: row;
  margin-left: 10rem;
  margin-right: 20rem;
}

.case .hero header nav ul li {
  font-size: 2rem;
  color: #434343;
  padding: 1rem;
  text-transform: uppercase;
}

.case .hero header nav .icons {
  margin-left: 0rem;
  margin-right: 0rem;
}

.case .hero header nav .icons i {
  font-size: 2rem;
  color: #434343;
  padding: 1rem;
}

.hero {
  height: 70vh;
}

.active {
  color: #ffffff;
  border-bottom: 1px solid #ffffff;
}

#toggle {
  display: none;
}

@media (max-width: 1024px) {
  .case .hero header {
    min-height: 8rem;
  }
  .case .hero header .logo {
    width: 22rem;
  }
  .case .hero header nav {
    min-height: 8rem;
    margin: 1rem 0;
  }
  .case .hero header nav ul {
    margin-left: 5rem;
    margin-right: 8rem;
  }
  .case .hero header nav ul li {
    font-size: 1.8rem;
    padding: 0.8rem;
  }
  .case .hero header nav .icons {
    margin-left: 0rem;
    margin-right: 0rem;
  }
  .case .hero header nav .icons i {
    font-size: 2rem;
    color: #434343;
    padding: 0.8rem;
  }
}

@media (max-width: 768px) {
  .case .hero header {
    min-height: 8rem;
  }
  .case .hero header .logo {
    width: 18rem;
  }
  .case .hero header nav {
    min-height: 8rem;
    margin: 1rem 0;
  }
  .case .hero header nav ul {
    margin-left: 5rem;
    margin-right: 5rem;
  }
  .case .hero header nav ul li {
    font-size: 1.6rem;
    padding: 0.5rem;
  }
  .case .hero header nav .icons {
    margin-left: 0rem;
    margin-right: 0rem;
  }
  .case .hero header nav .icons i {
    font-size: 2rem;
    color: #434343;
    padding: 0.5rem;
  }
}

@media (max-width: 640px) {
  .case .hero header {
    min-height: 8rem;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -ms-flex-direction: column;
    flex-direction: column;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    -webkit-box-align: end;
    -ms-flex-align: end;
    align-items: flex-end;
  }
  .case .hero header .logo {
    width: 18rem;
    display: none;
  }
  .case .hero header nav {
    min-height: 8rem;
    width: 100%;
    margin: 0;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -ms-flex-direction: column;
    flex-direction: column;
    position: absolute;
    top: 80px;
    background-color: #4281C1;
    z-index: 4;
    text-align: center;
    border-top: 2px solid #ffffff;
    border-bottom: 2px solid #ffffff;
    display: none;
    opacity: 0;
    -webkit-transition: all ease-in 0.7s;
    transition: all ease-in 0.7s;
  }
  .case .hero header nav ul {
    margin: 0.5rem 1rem;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -ms-flex-direction: column;
    flex-direction: column;
  }
  .case .hero header nav ul li {
    font-size: 1.6rem;
  }
  .case .hero header nav .icons {
    margin-left: 0rem;
    margin-right: 0rem;
    -webkit-box-orient: horizontal;
    -webkit-box-direction: normal;
    -ms-flex-direction: row;
    flex-direction: row;
  }
  .case .hero header nav .icons i {
    font-size: 2rem;
    color: #434343;
  }
  #toggle {
    font-size: 3rem;
    color: #ffffff;
    padding: 2rem;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
  }
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.1/css/all.css" integrity="sha384-xxzQGERXS00kBmZW/6qxqJPyxW3UR0BPsL4c8ILaIWXva5kFi7TxkIIaMiKtqV1Q" crossorigin="anonymous">

<div class="case">
  <section class="hero">
    <header>
      <i class="fas fa-bars" id="toggle"></i>
      <nav>
        <div class="logo">
          <img src="css/img/web/logo.png" alt="Logo kelimkac.cz">
        </div>
        <ul>
          <li><a href="#" class="active">Dom?</a></li>
          <li><a href="#">O nás</a></li>
          <li><a href="#">Produkty</a></li>
          <li><a href="#">Kontakt</a></li>
        </ul>
        <ul class="icons">
          <i class="fas fa-user"></i>
          <i class="fas fa-shopping-cart"></i>
        </ul>
      </nav>
    </header>
    <h1>Test</h1>
  </section>
</div>
question from:https://stackoverflow.com/questions/65890979/why-my-transition-on-my-hamburger-menu-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

// Hamburger menu
let toggle = document.getElementById("toggle");
let nav = document.querySelector("nav");

toggle.addEventListener('click', function() {
  nav.classList.toggle('active');
});
/* Global Styles */

@import url("https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap");
* {
  margin: 0;
  padding: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-decoration: none;
  font-family: 'Poppins', sans-serif;
}

html {
  font-size: 10px;
}

ul {
  list-style: none;
}

a {
  text-decoration: none;
  color: #434343;
  font-family: 'Poppins', sans-serif;
}

h1,
h2,
h3 {
  font-family: 'Poppins', sans-serif;
}

body {
  background-color: #ffffff;
}



.case {
  height: 100vh;
}

.case .hero {
  background-color: #ffffff;
}

.case .hero header {
  background-color: #4281C1;
  min-height: 10rem;
  width: 100%;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}

.case .hero header .logo {
  width: 28rem;
}

.case .hero header .logo img {
  width: 100%;
  height: 100%;
}

.case .hero header nav {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
  -ms-flex-direction: row;
  flex-direction: row;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  min-height: 10rem;
  margin: 1rem 0;
}

.case .hero header nav ul {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
  -ms-flex-direction: row;
  flex-direction: row;
  margin-left: 10rem;
  margin-right: 20rem;
}

.case .hero header nav ul li {
  font-size: 2rem;
  color: #434343;
  padding: 1rem;
  text-transform: uppercase;
}

.case .hero header nav .icons {
  margin-left: 0rem;
  margin-right: 0rem;
}

.case .hero header nav .icons i {
  font-size: 2rem;
  color: #434343;
  padding: 1rem;
}

.hero {
  height: 70vh;
}

.active {
  color: #ffffff;
  border-bottom: 1px solid #ffffff;
}

#toggle {
  display: none;
}

@media (max-width: 1024px) {
  .case .hero header {
    min-height: 8rem;
  }
  .case .hero header .logo {
    width: 22rem;
  }
  .case .hero header nav {
    min-height: 8rem;
    margin: 1rem 0;
  }
  .case .hero header nav ul {
    margin-left: 5rem;
    margin-right: 8rem;
  }
  .case .hero header nav ul li {
    font-size: 1.8rem;
    padding: 0.8rem;
  }
  .case .hero header nav .icons {
    margin-left: 0rem;
    margin-right: 0rem;
  }
  .case .hero header nav .icons i {
    font-size: 2rem;
    color: #434343;
    padding: 0.8rem;
  }
}

@media (max-width: 768px) {
  .case .hero header {
    min-height: 8rem;
  }
  .case .hero header .logo {
    width: 18rem;
  }
  .case .hero header nav {
    min-height: 8rem;
    margin: 1rem 0;
  }
  .case .hero header nav ul {
    margin-left: 5rem;
    margin-right: 5rem;
  }
  .case .hero header nav ul li {
    font-size: 1.6rem;
    padding: 0.5rem;
  }
  .case .hero header nav .icons {
    margin-left: 0rem;
    margin-right: 0rem;
  }
  .case .hero header nav .icons i {
    font-size: 2rem;
    color: #434343;
    padding: 0.5rem;
  }
}

@media (max-width: 640px) {
  .case .hero header {
    min-height: 8rem;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -ms-flex-direction: column;
    flex-direction: column;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    -webkit-box-align: end;
    -ms-flex-align: end;
    align-items: flex-end;
  }
  .case .hero header .logo {
    width: 18rem;
    display: none;
  }
  .case .hero header nav {
    min-height: 8rem;
    width: 100%;
    margin: 0;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -ms-flex-direction: column;
    flex-direction: column;
    position: absolute;
    top: 80px;
    background-color: #4281C1;
    z-index: 4;
    text-align: center;
    border-top: 2px solid #ffffff;
    border-bottom: 2px solid #ffffff;
    visibility: hidden;
    opacity: 0;
    -webkit-transition: all ease-in 0.7s;
    transition: all ease-in 0.7s;
  }
  .case .hero header nav.active {
    opacity: 1;
    visibility: visible;
  }
  .case .hero header nav ul {
    margin: 0.5rem 1rem;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -ms-flex-direction: column;
    flex-direction: column;
  }
  .case .hero header nav ul li {
    font-size: 1.6rem;
  }
  .case .hero header nav .icons {
    margin-left: 0rem;
    margin-right: 0rem;
    -webkit-box-orient: horizontal;
    -webkit-box-direction: normal;
    -ms-flex-direction: row;
    flex-direction: row;
  }
  .case .hero header nav .icons i {
    font-size: 2rem;
    color: #434343;
  }
  #toggle {
    font-size: 3rem;
    color: #ffffff;
    padding: 2rem;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
  }
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.1/css/all.css" integrity="sha384-xxzQGERXS00kBmZW/6qxqJPyxW3UR0BPsL4c8ILaIWXva5kFi7TxkIIaMiKtqV1Q" crossorigin="anonymous">

<div class="case">
  <section class="hero">
    <header>
      <i class="fas fa-bars" id="toggle"></i>
      <nav>
        <div class="logo">
          <img src="css/img/web/logo.png" alt="Logo kelimkac.cz">
        </div>
        <ul>
          <li><a href="#" class="active">Dom?</a></li>
          <li><a href="#">O nás</a></li>
          <li><a href="#">Produkty</a></li>
          <li><a href="#">Kontakt</a></li>
        </ul>
        <ul class="icons">
          <i class="fas fa-user"></i>
          <i class="fas fa-shopping-cart"></i>
        </ul>
      </nav>
    </header>
    <h1>Test</h1>
  </section>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...