简体   繁体   English

jquery 可拖动元素克隆和 static

[英]jquery draggable elements cloned and static

I'm using the "draggable" function of JQUERY.我正在使用 JQUERY 的“可拖动”function。 How can I make some of my <div> elements to be cloned?如何使我的一些<div>元素被克隆?

These are my examples, both are important to be working together.这些是我的例子,两者一起工作都很重要。

Actually working:实际工作:

<div class="dragzones">Drag me, but im unique okay?</div>

This one is not working, this is my goal!这个不行,这是我的目标!

<div class="dragzones_clone">I want to be dragged and cloned too!</div>

Important: I NEED BOTH ways!重要提示:我需要两种方式!

This is the code I am using:这是我正在使用的代码:

   $ (init);
  function init() {
    $(".dragzones").draggable({
      start: handleDragStart,
      cursor: 'move',
      revert: "invalid",
    }
                             );
    $(".dropzones").droppable({
      drop: handleDropEvent           
    }
                             );
    validateDropzones();
  }
  function handleDragStart (event, ui) {
    $(this).css('z-index', 9999);
  }
  function handleDropEvent (event, ui) {
    if ($(this).hasClass('occupied')) {
      ui.draggable.draggable('option', 'revert', true);
      return false;
    }
    $(this).append(ui.draggable);
    ui.draggable.position({
      of: $(this), my: 'left top', at: 'left top'}
                         );
    ui.draggable.css('z-index', 0);
    setTimeout(validateDropzones, 0);
  }
  function validateDropzones() {
    $(".dropzones").each(function(){
      if ($(".dragzones", this)[0]) {
        $(this).addClass("occupied");
      }
      else {
        $(this).removeClass("occupied");
      }
    }
  );
}

I solved my problem.我解决了我的问题。

Maybe this can help, if someone finds this post and need my solution.如果有人找到这篇文章并需要我的解决方案,也许这会有所帮助。

  $ (init);
  function init() {
    $(".clonezone").draggable({
        start: handleDragStart,
        helper: "clone",
        revert: "true",
    });

    $(".dragzones").draggable({
        start: handleDragStart,
        cursor: 'move',
        revert: "invalid",
    });

    $(".dropzones").droppable({
        accept: ".dragzones, .clonezone",
        drop: function (event, ui) {
        var droppable = $(this);
        var draggable = ui.draggable;
        var className = ui.draggable.attr("class").split(' ')[0] 

        if(className==("clonezone")){
            draggable.clone().appendTo(droppable);
            ui.draggable.css('z-index', 0);
            setTimeout(validateDropzones, 0);
        }
        else{
            if ($(this).hasClass('occupied')) {
            ui.draggable.draggable('option', 'revert', true);
            return false;
            }
            $(this).append(ui.draggable);
            ui.draggable.position({of: $(this), my: 'left top', at: 'left top'});
            ui.draggable.css('z-index', 0);
            setTimeout(validateDropzones, 0);
        }
      }
    }
   );
    validateDropzones();
  }
  function handleDragStart (event, ui) {
    $(this).css('z-index', 9999);
  }
  function validateDropzones() {
    $(".dropzones").each(function(){
      if ($(".dragzones, .clonezone", this)[0]) {
        $(this).addClass("occupied");
      }
      else {
        $(this).removeClass("occupied");
      }
    }
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM