javascript - How to make the transition smooth for the hamburger menu in responsive navbar when toggled? - Stack Overflow

I'm making a responsive navbar with a hamburger menu that toggles the navbar when clicked on the H

I'm making a responsive navbar with a hamburger menu that toggles the navbar when clicked on the Hamburger Icon. I'm trying to make the dropdown of the Nav menu smooth using transition property in CSS. But there is no change in the transition while toggling.

I have tried adding height property to the navbar but still, there is no transition.

var toggleButton = document.querySelector('.toggle-btn');
var navLinks = document.querySelector('.nav-main');

toggleButton.addEventListener('click', function() {
  navLinks.classList.toggle('hidden');
});
.navbar {
  background: #000;
  font-size: 26px;
  font-family: 'Play', sans-serif;
  padding-top: 35px;
  padding-bottom: 20px;
}

.nav-main {
  display: none;
}

.nav-main li {
  list-style: none;
  text-align: center;
  margin: 10px auto;
}

.nav-links {
  text-decoration: none;
  color: #F5F5F5;
}

.nav-links:hover {
  color: #E6E6E6;
}

.toggle-btn {
  position: absolute;
  top: 15px;
  right: 35px;
  cursor: pointer;
  color: #F5F5F5;
}

.toggle-btn:hover {
  color: #E6E6E6;
}

.hidden {
  display: block;
}
<link href=".8.1/css/all.css" rel="stylesheet"/>

<nav class="navbar">
  <span class="toggle-btn"><i class="fas fa-bars"></i></span>
  <ul class="nav-main">
    <li><a href="#" class="nav-links">Home</a></li>
    <li><a href="#" class="nav-links">Portfolio</a></li>
    <li><a href="#" class="nav-links">About</a></li>
    <li><a href="#" class="nav-links">Blog</a></li>
    <li><a href="#" class="nav-links">Contact</a></li>
  </ul>
</nav>

I'm making a responsive navbar with a hamburger menu that toggles the navbar when clicked on the Hamburger Icon. I'm trying to make the dropdown of the Nav menu smooth using transition property in CSS. But there is no change in the transition while toggling.

I have tried adding height property to the navbar but still, there is no transition.

var toggleButton = document.querySelector('.toggle-btn');
var navLinks = document.querySelector('.nav-main');

toggleButton.addEventListener('click', function() {
  navLinks.classList.toggle('hidden');
});
.navbar {
  background: #000;
  font-size: 26px;
  font-family: 'Play', sans-serif;
  padding-top: 35px;
  padding-bottom: 20px;
}

.nav-main {
  display: none;
}

.nav-main li {
  list-style: none;
  text-align: center;
  margin: 10px auto;
}

.nav-links {
  text-decoration: none;
  color: #F5F5F5;
}

.nav-links:hover {
  color: #E6E6E6;
}

.toggle-btn {
  position: absolute;
  top: 15px;
  right: 35px;
  cursor: pointer;
  color: #F5F5F5;
}

.toggle-btn:hover {
  color: #E6E6E6;
}

.hidden {
  display: block;
}
<link href="https://use.fontawesome./releases/v5.8.1/css/all.css" rel="stylesheet"/>

<nav class="navbar">
  <span class="toggle-btn"><i class="fas fa-bars"></i></span>
  <ul class="nav-main">
    <li><a href="#" class="nav-links">Home</a></li>
    <li><a href="#" class="nav-links">Portfolio</a></li>
    <li><a href="#" class="nav-links">About</a></li>
    <li><a href="#" class="nav-links">Blog</a></li>
    <li><a href="#" class="nav-links">Contact</a></li>
  </ul>
</nav>

Share Improve this question edited Apr 2, 2019 at 18:11 Jack 9,3882 gold badges34 silver badges45 bronze badges asked Apr 2, 2019 at 17:43 Alex AntonyAlex Antony 911 gold badge3 silver badges8 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

To animate height by transition you should change max-height property. You can take a look at this example here: https://codepen.io/felipefialho/pen/ICkwe

For your case:

1) set max-height: 0 for .nav-main

2) set max-height: ${your max height value here} for .nav-main--open

3) change this row navLinks.classList.toggle('nav-main--open');

Probably it will be useful for you.

Inspired by this post: https://stackoverflow./a/25544161/2777988

I used a label and a hidden checkbox to implement a smooth linear transition. This doesn't even require any Javascript.

#block {
  background: #000;
  height: 0;
  overflow: hidden;
  transition: height 300ms linear;
  font-size: 26px;
  font-family: 'Play', sans-serif;
}

label {
  cursor: pointer;
}

#showblock {
  display: none;
}

#showblock:checked+#block {
  height: 240px;
}

.navbar {
  background: #000;
  font-size: 26px;
  padding-top: 35px;
  padding-bottom: 10px;
  font-family: 'Play', sans-serif;
}

.nav-main {
  display: block;
}

.nav-main li {
  list-style: none;
  text-align: center;
  margin: 10px auto;
}

.nav-links {
  text-decoration: none;
  color: #F5F5F5;
}

.nav-links:hover {
  color: #E6E6E6;
}

.toggle-btn {
  position: absolute;
  top: 15px;
  right: 35px;
  cursor: pointer;
  color: #F5F5F5;
}

.toggle-btn:hover {
  color: #E6E6E6;
}

.hidden {
  display: block;
}
<link href="https://use.fontawesome./releases/v5.8.1/css/all.css" rel="stylesheet" />
<nav class="navbar">
  <span class="toggle-btn"><label for="showblock" class="fas fa-bars"></label></span>
</nav>
<input type="checkbox" id="showblock" />
<div id="block">
  <ul class="nav-main">
    <li><a href="#" class="nav-links">Home</a></li>
    <li><a href="#" class="nav-links">Portfolio</a></li>
    <li><a href="#" class="nav-links">About</a></li>
    <li><a href="#" class="nav-links">Blog</a></li>
    <li><a href="#" class="nav-links">Contact</a></li>
  </ul>
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744760705a4592157.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信