1

I need to animate a responsive top tab navigator in CSS (the typical open/close transisition)

The problem I am having is that I cannot make a transition when, after pressing the toggle button, which open/closes the tab navigator, its height changes from 85px to unset.

Any ideas?

Here is my current code:

(ALERT: Test on small screen, to see the responsify)

function responsifyTopNavigator() {
  const nav = document.getElementById("topnav");

  let navCassName = "topnav";
  if (nav.className === "topnav") {
    navCassName += " responsive";
  }

  nav.className = navCassName;
}
* {
  margin: 0;
  padding: 0;
}

.topnav {
  position: fixed;
  overflow: hidden;
  top: 0;
  width: 100%;
  height: 85px;
  background-color: #f0efe9;
  transition: height 0.1s ease-in;
  -webkit-transition: height 0.1s ease-in;
  -moz-transition: height 0.1s ease-in;
  -o-transition: height 0.1s ease-in;
}
.topnav #routes-container {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
}
.topnav a {
  float: left;
  display: block;
  height: 20px;
  padding: 35px 30px;
  font-size: 16px;
  text-align: center;
  text-decoration: none;
  color: #41403e;
}
.topnav a#toggle {
  display: none;
}


@media screen and (max-width: 1000px) {
  .topnav a {
    font-size: 16px;
  }
  .topnav #routes-container {
    display: none;
  }
  .topnav a#toggle {
    float: right;
    display: block;
  }
}
@media screen and (max-width: 1000px) {
  .topnav.responsive {
    position: relative;
    height: unset;
  }
  .topnav.responsive a#toggle {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive #routes-container {
    display: block;
    margin-top: 85px;
  }
  .topnav.responsive #routes-container a {
    float: none;
    text-align: left;
  }
}
<div class="topnav" id="topnav">
  <div id="routes-container">
    <a href="#" class="active">1</a>
    <a href="#">2</a>
    <a href="#">3</a>
    <a href="#">4</a>
    <a href="#">5</a>
  </div>

  <a
    href="javascript:void(0);"
    id="toggle"
    onclick="responsifyTopNavigator()"
  >
    TOGGLE
  </a>
</div>
Raul
  • 2,673
  • 1
  • 15
  • 52

0 Answers0