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

94
Visualizações
Moving divs on click to new position

I have three divs in a container next to each other and I'm trying to get them to change their positions by clicking them. I'd like to be able to, for example, click on the left div and have it move to the center position. Here is what I have so far. I have one div animating(I removed the other two divs since I couldn't get them to animate).

HTML

var checker = true;
$("#div1").click(function () {
  targetLeft = checker ? "10%" : "30%";
  $(this).animate({left: targetLeft},400);   
  checker ? checker = false : checker = true;
});
#contentdiv {
  background-color: grey;
  border:1px solid black;
  height:150px;
  width:500px;
}

#div1  {  
  background-color: purple;
  height:100px;
  width:100px;
  position:absolute;
  display: inline-block;
  left:30%;
  border-radius: 12px;
}
<div id="contentdiv">
  <div id="div1">div1</div>
</div>

JSFIDDLE

Thank you for any help you can provide. Much appreciated :)

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

0

You have the documentation online with exemple for animate().

var checker = true;
$("#div1").click(function () {
targetLeft = checker ? "10%" : "30%";
$(this).animate({left: targetLeft},400);   
checker ? checker = false : checker = true;
});

$("#div2").click(function () {
  let checkTop = !$(this).hasClass('div2active') ? "50%" : $("#contentdiv").offset().top + 'px';
  let checkLeft = !$(this).hasClass('div2active') ? "50%" : "40%";
  
  $(this).animate({
    left: checkLeft,
    top: checkTop
  },400).toggleClass('div2active');
});
#contentdiv {
  background-color: grey;
  border:1px solid black;
  height:500px;
  width:100%;
}

.divs {
  height:100px;
  width:100px;
  position:absolute;
  display: inline-block;
  border-radius: 12px;
}

#div1  {  
  background-color: purple;
  left:30%;
}
#div2  {  
  background-color: teal;
  left:40%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="contentdiv">
  <div id="div1" class="divs">div1</div>
  <div id="div2" class="divs">div2</div>
</div>

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