我正在开发一个简单的应用程序,该应用程序会将圆圈放入盒子中。 我想要的是,将所有这些圆移动到新容器中,我尝试使用相同的代码,但是方向不正确。
您需要选择一个圆圈,然后选择其中一个框,然后可以再次选择。之后,点击“移动”以转移所有这些 圈到顶部的那个大盒子。
var selectedCircle = null;
$(".circle").click(function() {
$(this).addClass('selected').siblings().siblings('.clone').remove();
$(this).addClass('selected').siblings().removeClass('selected').animate({
top: 0,
left: 0
});
selectedCircle = $(this);
});
$(".container .square").click(function() {
if (selectedCircle) {
$('.clone').removeClass('selected');
var selectedSquare = $(this);
debugger;
var selectedCircleClone = selectedCircle.clone().addClass("clone");
selectedCircle.after(selectedCircleClone);
var xOffset = (selectedSquare.width() - selectedCircleClone.width()) / 2;
var yOffset = (selectedSquare.height() - selectedCircleClone.height()) / 2;
selectedCircleClone.animate({
top: selectedSquare.offset().top - selectedCircleClone.offset().top + yOffset,
left: selectedSquare.offset().left - selectedCircleClone.offset().left + xOffset
}, 1200);
}
});
$(".move").click(function() {
var main = $('.main');
var clones = $('.round .clone');
var xOffset = (main.width() - clones.width()) / 2;
var yOffset = (main.height() - clones.height()) / 2;
clones.animate({
top: main.offset().top - clones.offset().top + yOffset,
left: main.offset().left - clones.offset().left + xOffset
}, 1200);
});
.main{
width: 100px;
height: 100px;
background-color: #DDD;
margin: 0 auto;
}
.container {
height: 200px;
width: 550px;
background-color: #eee;
padding: 10px;
position: relative;
border: 1px solid #DDD;
margin: auto;
}
.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: absolute;
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;
}
.round .box{
width: 45px;
height: 45px;
float: left;
position: relative;
}
.move{
cursor: pointer;
background-color: #FFF;
border: 1px solid #DDD;
height: 30px;
width: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
</div>
<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="box">
<div class="circle">
<span>1</span>
</div>
</div>
<div class="box">
<div class="circle">
<span>2</span>
</div>
</div>
<div class="box">
<div class="circle">
<span>3</span>
</div>
</div>
<div class="box">
<div class="circle">
<span>4</span>
</div>
</div>
</div>
<div class="move">MOVE</div>
</div>