如果我的可拖动对象恢复到某个位置,我正在尝试更改它的颜色。在此示例中,当您将红色框拖动到右侧的绿色框时,它会变为蓝色。我已经设置了,所以当你将一个盒子拖到另一个盒子上时,它会恢复到原来的位置。但是,如果框恢复到左侧的容器,我希望它变回红色,如果你手动将它拖到那里。我也可以做到,所以如果一个盒子在左边的容器里,它总是红色的,但我不知道该怎么做。任何帮助是极大的赞赏。提前致谢。
这是HTML:
<div id="box">
<div class="peg"> </div>
<div class="space"> </div>
<div class="peg"> </div>
</div>
<div id="game">
<ul class="row">
<li class="hole"> </li>
<li class="hole"> </li>
</ul>
</div>
CSS:
#game {
position: absolute;
top: 15px;
left: 135px;
background: wheat;
padding: 15px;
padding-bottom: 0;
}
.row {
position: relative;
margin-left: auto;
margin-right: auto;
height: 75px;
margin-bottom: 15px;
background-color: olive;
}
.hole {
width: 75px;
height: 75px;
float: left;
background-color: aqua;
opacity: 0.3;
margin-left: 6px;
}
#box {
position: absolute;
left: 15px;
top: 15px;
width: 90px;
padding-bottom: 15px;
background-color: wheat;
padding-top: 15px;
padding-left: 15px;
}
.peg {
width: 75px;
height: 75px;
background-color: red;
z-index: 1;
}
.space {
height: 15px;
width: 75px;
}
.top {
background-color: blue;
width: 75px;
height: 75px;
z-index: 1;
}
Javascript:
$('.peg' ).draggable({
snap: ".hole",
snapMode: "inner",
snapTolerance: 40,
//scope: "zappa",
//revert: 'invalid',
stop: function(){
$(this).draggable('option','revert','invalid');
},
//helper: "clone"
});
$('.peg').droppable({
tolerance: 'fit'
});
$('.peg').droppable({
greedy: true,
tolerance: 'touch',
drop: function(event,ui){
ui.draggable.draggable('option','revert',true);
}
});
$('.hole').droppable({
drop: function(e,ui) {
$(ui.draggable).removeClass('peg').addClass('top');
},
//scope: "zappa"
});
$('#box').droppable({
drop: function(e,ui) {
$(ui.draggable).removeClass('top').addClass('peg');
},
//scope: "zappa"
});