Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CSS transition does not work when I toggle the class with JS

Problem: I want to slide a searchbar from top to bottom with one click. I use a clickEvent with which I toggle the class show to the search input field. The class show only contains the target position.

Goal: I want the search input field to slide down. Currently it jumps from top to bottom.

Question Why doesn’t it work and what do I have to do to make it work?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const sbtn = document.querySelector('.nav__search-btn'),
sbar = document.querySelector('.searchbar__input');
sbtn.addEventListener('click', () => {
  sbar.classList.toggle('show');
});
* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
  position: relative;
}

.nav {
  display: flex;
  justify-content: space-between;
  background: lightgray;
  height: 50px;
  align-items: center;
}

.right {
  display: flex;
  justify-content: space-between;
  align-items: center;
  text-align: center;
  gap: 20px;
  height: 100%;
}

.btn {
  background: green;
  padding: 10px;
}


.searchbar {
  position: absolute;
  top: 0px;
  left: 10%;
  width: 80%;
  transition: top 1s ease 0s;
}

.searchbar__input {
  width: 100%;
  height: 30px;
  transition: all 2s;
}

.show {
  position: relative;
  top: 50px;
}
<nav>
  <div class="nav">
    <div></div>
    <div class="right">
      <div class="btn nav__search-btn">Search</div>
    </div>
  </div>
  <div class="searchbar">
    <input class="searchbar__input"/>
  </div>
</nav>

>Solution :

.searchbar__input also needs to have a top property.

You can also add position: relative; to .searchbar__input:


Edit: You don’t need the position property on .searchbar__input since you’re adding it to all in the global rule.

const sbtn = document.querySelector('.nav__search-btn'),
sbar = document.querySelector('.searchbar__input');
sbtn.addEventListener('click', () => {
  sbar.classList.toggle('show');
});
* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
  position: relative;
}

.nav {
  display: flex;
  justify-content: space-between;
  background: lightgray;
  height: 50px;
  align-items: center;
}

.right {
  display: flex;
  justify-content: space-between;
  align-items: center;
  text-align: center;
  gap: 20px;
  height: 100%;
}

.btn {
  background: green;
  padding: 10px;
}


.searchbar {
  position: absolute;
  top: 0px;
  left: 10%;
  width: 80%;
  transition: top 1s ease 0s;
}

.searchbar__input {
  width: 100%;
  height: 30px;
  top: 0;
  transition: all 2s;
}

.show {  
  top: 50px;
}
<nav>
  <div class="nav">
    <div></div>
    <div class="right">
      <div class="btn nav__search-btn">Search</div>
    </div>
  </div>
  <div class="searchbar">
    <input class="searchbar__input"/>
  </div>
</nav>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading