Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

91
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda