How to replicate a draggable/droppable instance in jquery after first instance completed

空扰寡人 提交于 2020-01-05 10:11:01

问题


I have a small jquery draggable/droppable app I am building. You can see it here:

http://jsfiddle.net/franco13/AwnJA/1/

I need to do the following and I am new to dragging and dropping in jquery, so thank you for assisting.

I wish to:

  1. prevent the blue box from being dragged to box 2, until it has been dropped in box 1
  2. once the blue box is dropped into box 1, I want to make it draggable again so it can be dropped into box 2 while leaving a clone behind

like this:

$( init );

function init() {

  $('.teamEmblem').draggable({
//    containment: $('.teamEmblem').parent(), // this does not work
    cursor: 'move',
    helper: 'clone',
    obstacle: ".teamEmblem", // this does not work
    preventCollision: true, // this does not work
    revert: true
  });

  $('.winner').droppable({
    hoverClass: 'hovered',
    tolerance: 'touch',
    drop: handleCardDrop2
  });

}


function handleCardDrop2( event, ui ) {
    if (true) {

        ui.draggable.addClass('correct');
        ui.draggable.draggable('disable');
        $(this).droppable('disable');

        var dragged = ui.draggable.clone(true);
        dragged.position({
            of: $(this),
            my: 'left top',
            at: 'left top'
        }).css({
            position: 'absolute',
            display: 'block',
            margin: '0 auto'
        });
        ui.draggable.draggable('option', 'revert', false);
        $('body').append(dragged);
    }

}

回答1:


Just little changes are necessary from your starting code, something like that should work but im not sure it fit all your needs:

{im not sure you want the original element to be draggable again or the cloned element dragged in box1}

SEE DEMO

function handleCardDrop2(event, ui) {
    if (true) {
        var $dragged = ui.draggable,
            $draggedClone = $dragged.clone(),
            $target = $(event.target);

        if ($target.is('.box1')) $dragged.addClass('doppedBox1 correct');
        else if ($target.is('.box2') && $dragged.is(':not(.doppedBox1)')) return false;
        else if ($target.is('.box2')) $dragged.addClass('doppedBox2').draggable('disable');

        $(this).droppable('disable');

        $draggedClone.position({
            of: $(this),
            my: 'left top',
            at: 'left top'
        }).css({
            position: 'absolute',
            display: 'block',
            margin: '0 auto'
        });
        ui.draggable.draggable('option', 'revert', false);
        $('body').append($draggedClone);
    }
}



回答2:


To answer part 1 I came up with something like this fiddle

The basic concepts are:

  • add an index for your rounds as they are inferred and not explicit enough
  • attach a data object to each object that can move through the rounds and track what they have accomplished in it
  • test their data object

like this. Note that i re-defined the li to have a prereq as an attribute but you could use data for that too <li prereq="in1" class="winner first">box 1</li>)

var order = ['in1', 'in2', 'in3'];  // round index
// see your code
$('.temEmblem').draggable({blah}).data('complete', {
    'in1': true,
    'in2': false,
    'in3': false
});  // attach data element to draggable element

// see your code

function handleCardDrop2(e, ui){

    // see your handleCardDrop2 code

    var currentRd = $(this).attr('prereq');
    var nextRd = order[order.indexOf($(this).attr('prereq')) + 1];
    if (ui.draggable.data('complete')[currentRd]) {

        ui.draggable.data('complete', {
            nextRd: true
        });
        // do the drop
    } else { 
        // bail 
    }

    // handleCardDrop2 code

}); 


来源:https://stackoverflow.com/questions/16263164/how-to-replicate-a-draggable-droppable-instance-in-jquery-after-first-instance-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!