Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
How to make button slide with animated collapsible div?

I have a div which collapses on a button click. Code as follows:

const collapse = () => {
  const title = document.getElementById('title');
  title.classList.add('d-none');
  const out = document.getElementById('out');
  out.classList.add('collapse');
}
.out {
  width: 200px;
  background-color: grey;
  display: flex;
  justify-content: space-between;
}

.d-none {
  display: none;
}

.collapse {
  width: 65px;
}

.btn {
  width: 65px;
}
<div id='out' class='out'>
  <span id='title'>Title</span>
  <button class='btn' onclick='collapse()'>Collapse</button>
</div>

I'd like to add a transition effect, so that the div would collapse smoothly, the button would move smoothly to the left end with div collapse. How can I do that?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

EDIT I may have misunderstood what your intended outcome is. If you comment on here what you'd like it to do, I can make it more correctly align. :)

I've added an example with what you have, but I'd recommend just adding and removing a class based on the state.

Basically, you just toggle the 'collapse' classname, and add transition properties to the div class, as well as the collapse class. You can see the code below:

const collapse = () => {
  const out = document.getElementById('out');
  // We only have to toggle the collapse class
  out.classList.toggle('collapse');
}
.out {
  width: 200px;
  background-color: grey;
  display: inline-block;
  /* This transition happens when the collapse is removed */
  transition: all 300ms ease-in-out;
}

.d-none {
  height: 0;
}

.collapse {
  width: 0;
  /* This transition happens when the collapse class is added */
  transition: all 300ms ease-in-out;
}

.btn {
  width: auto;
  margin-left: -5px;
}
<div id='out' class='out'>
  <span id='title'>Title</span>
</div>
<button class='btn' onclick='collapse()'>Collapse</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda