Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

90
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!