动画的居中元素

时间:2018-07-20 03:01:53

标签: jquery

我正在开发一个简单的应用程序,该程序可以在选定的正方形内移动圆,首先,您必须选择需要移动的圆,然后选择应该放置的正方形,元素的移动还可以,我的问题是,即使在动画之后,元素在执行动画时也不会居中。

希望你能帮助我。

谢谢。

var selectedCircle = null;

$(".circle").click(function() {
    $(this).addClass('selected').siblings().removeClass('selected').animate({ top: 0, left: 0 });
    selectedCircle = $(this);
});

$(".container .square").click(function() {
  if (selectedCircle) {
    var selectedSquare = $(this);
    selectedSquare.addClass("selected");
    // 2.5 is for centering the circle in the square : width/height of square - width/height of circle / 2
    // => (50 - 45) / 2
    selectedCircle.animate({
      top: selectedSquare.offset().top - selectedCircle.offset().top + 2.5,      
      left: selectedSquare.offset().left - selectedCircle.offset().left + 2.5
    }, 1200);
  }
  selectedCircle = null;
});
.container{
  height: 200px;
  width: 550px;
  background-color: #eee;
  padding: 10px;
  position: relative;
  border: 1px solid #DDD;
}
.round{
  position: absolute;
  bottom: 10px;
}
.square{
  cursor: pointer;
  display: inline-block;
  width: 50px;
  height: 50px;
  border: 1px solid #f60;
  position: relative;
}
.square:nth-child(2){
   width: 65px;
}
.square:nth-child(3){
   width: 75px;
}
.square:nth-child(4){
   width: 85px;
}
.square:nth-child(5){
   width: 95px;
}
.square:nth-child(6){
   width: 105px;
}
.square:nth-child(7){
   width: 115px;
}
.square:nth-child(8){
   width: 125px;
}
.square .circle{
    position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
}
.circle{
  cursor: pointer;
  display: inline-block;
  width: 45px;
  height: 45px;
  border: 1px solid green;
  border-radius: 100px;
  text-align: center;
  position: relative;
}
.circle span{
  width: 10px;
  height: 20px;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
}
.circle.selected{
  background-color: #FFFF;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
 <div class="box">
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
</div>
 <div class="round">
   <div class="circle">
     <span>1</span>
   </div>
   <div class="circle">
     <span>2</span>
   </div>
   <div class="circle">
     <span>3</span>
   </div>
   <div class="circle">
     <span>4</span>
   </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

您在代码中注释了正确的计算:

width/height of square - width/height of circle / 2

您只需要执行该计算,而不用对2.5进行硬编码:

var xOffset = (selectedSquare.width() - selectedCircle.width()) / 2;
var yOffset = (selectedSquare.height() - selectedCircle.height()) / 2;

var selectedCircle = null;

$(".circle").click(function() {
    $(this).addClass('selected').siblings().removeClass('selected').animate({ top: 0, left: 0 });
    selectedCircle = $(this);
});

$(".container .square").click(function() {
  if (selectedCircle) {
    var selectedSquare = $(this);
    selectedSquare.addClass("selected");

    var xOffset = (selectedSquare.width() - selectedCircle.width()) / 2;
    var yOffset = (selectedSquare.height() - selectedCircle.height()) / 2;

    selectedCircle.animate({
      top: selectedSquare.offset().top - selectedCircle.offset().top + yOffset,      
      left: selectedSquare.offset().left - selectedCircle.offset().left + xOffset
    }, 1200);
  }
  selectedCircle = null;
});
.container{
  height: 200px;
  width: 550px;
  background-color: #eee;
  padding: 10px;
  position: relative;
  border: 1px solid #DDD;
}
.round{
  position: absolute;
  bottom: 10px;
}
.square{
  cursor: pointer;
  display: inline-block;
  width: 50px;
  height: 50px;
  border: 1px solid #f60;
  position: relative;
}
.square:nth-child(2){
   width: 65px;
}
.square:nth-child(3){
   width: 75px;
}
.square:nth-child(4){
   width: 85px;
}
.square:nth-child(5){
   width: 95px;
}
.square:nth-child(6){
   width: 105px;
}
.square:nth-child(7){
   width: 115px;
}
.square:nth-child(8){
   width: 125px;
}
.square .circle{
    position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
}
.circle{
  cursor: pointer;
  display: inline-block;
  width: 45px;
  height: 45px;
  border: 1px solid green;
  border-radius: 100px;
  text-align: center;
  position: relative;
}
.circle span{
  width: 10px;
  height: 20px;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
}
.circle.selected{
  background-color: #FFFF;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
 <div class="box">
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
</div>
 <div class="round">
   <div class="circle">
     <span>1</span>
   </div>
   <div class="circle">
     <span>2</span>
   </div>
   <div class="circle">
     <span>3</span>
   </div>
   <div class="circle">
     <span>4</span>
   </div>
  </div>
</div>