克隆所选元素 [英] Clone selected element
本文介绍了克隆所选元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在工作一个简单的应用程序,该应用程序可在元素内移动元素. 代码工作正常,我只想克隆一个圆圈,以便可以将相同的元素放在不同的盒子上.
I'm working a simple app that move an element inside an element. The code is working fine, I just want to make a clone of the circles so that I can place same element on different boxes.
我尝试添加此行
$( selectedChip ).clone().after( selectedChip );
这样,每次我单击正方形时,都会插入clone元素,而原始元素会保留.
so that every time I click on the squares the clone element will be inserted and the original stays.
希望你能帮助我.
谢谢.
var selectedCircle = null;
$(".circle").click(function() {
$(this).addClass('selected').siblings().removeClass('selected').animate({ top: 0, left: 0 });
selectedCircle = $(this);
});
$(".container .square").click(function() {
// $( selectedChip ).clone().after( selectedChip );
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);
}
});
.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>
推荐答案
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) {
var selectedSquare = $(this);
debugger;
var selectedCircleClone = selectedCircle.clone().addClass("clone");
selectedCircle.after(selectedCircleClone);
selectedSquare.addClass("selected");
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);
}
});
.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>
这篇关于克隆所选元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文