jQuery - 仅在删除时克隆div

时间:2017-09-22 11:07:41

标签: jquery jquery-ui jquery-ui-droppable

我怎样才能在删除时创建某个div的“克隆”?当你开始拖动并准备好再次拖动时,这个克隆应该在同一个点上。

例如,在这个fiddle中,可以在初始位置克隆绿色元素,只有在它们被删除时?

这是我的JS:

$(function() {
  $("#dragrabble-one").draggable({
    helper: 'clone'
  });
  $("#dragrabble-two").draggable({
    helper: 'clone'
  });
  $("#dragrabble-three").draggable({
    helper: 'clone'
  });
  $("#dragrabble-four").draggable({
    helper: 'clone'
  });
  $("#dragrabble-five").draggable({
    helper: 'clone'
  });
  $("#dragrabble-six").draggable({
    helper: 'clone'
  });
  $("#dragrabble-seven").draggable({
    helper: 'clone'
  });
  $("#dragrabble-eight").draggable({
    helper: 'clone'
  });
  $("#dragrabble-one").draggable({
    revert: "invalid"
  });
  $("#dragrabble-two").draggable({
    revert: "invalid"
  });
  $("#dragrabble-three").draggable({
    revert: "invalid"
  });
  $("#dragrabble-four").draggable({
    revert: "invalid"
  });
  $("#dragrabble-five").draggable({
    revert: "invalid"
  });
  $("#dragrabble-six").draggable({
    revert: "invalid"
  });
  $("#dragrabble-seven").draggable({
    revert: "invalid"
  });
  $("#dragrabble-eight").draggable({
    revert: "invalid"
  });
  $("#droppable-box").droppable({
    drop: function(event, ui) {
      var clone = ui.helper.clone();
      clone.draggable();
      clone.appendTo('#droppable-box');
    }
  });
});

提前致谢。

1 个答案:

答案 0 :(得分:0)

A, 一个解决方案可能是,当用户开始拖动元素并在拖动停止时将不透明度设置回1时,隐藏不透明度等于0的克隆元素。

$("#dragrabble-one").draggable({

    helper: 'clone',
    start: function (event, ui){
        $(this).css('opacity','0');
    },
    stop: function (event, ui){
        $(this).css('opacity','1');
    }

});

我希望这会对你有所帮助! :)