0

我已经建立了一个 jsFiddle 来说明这里的整个情况。一切都如我所愿,除了当你把红色的“钉子”拖到右边的一个绿色盒子上,然后在它上面拖另一个红色钉子时,我希望它恢复时会变回红色回到左边的盒子。

HTML:

<div id="box">
    <div class="peg">&nbsp;</div>
    <div class="space">&nbsp;</div>
    <div class="peg">&nbsp;</div>
    <div class="space">&nbsp;</div>
    <div class="peg">&nbsp;</div>
    <div class="space">&nbsp;</div>
    <div class="peg">&nbsp;</div>
</div>



<div id="game">
    <ul class="row">
        <li class="hole">&nbsp;</li>
        <li class="hole">&nbsp;</li>
        <li class="hole">&nbsp;</li>
        <li class="hole">&nbsp;</li>
    </ul>
    <ul class="row">
        <li class="hole">&nbsp;</li>
        <li class="hole">&nbsp;</li>
        <li class="hole">&nbsp;</li>
        <li class="hole">&nbsp;</li>
    </ul> </div>

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"
});

CSS:

#game {
    position: absolute;
    width: 330px;
    height: 400px;
    top: 15px;
    left: 135px;
    background: wheat;
    padding: 15px;
}

.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;
}
4

0 回答 0