1

jquery를 가르치기 위해 드래그 앤 드롭 게임을 만들었습니다. 모든 것이 효과가있는 것으로 보입니다. 그러나 같은 사각형 위에 하나 이상의 아이템을 떨어 뜨릴 수 있습니다. 장소 홀더에 이미지가있는 경우 삭제할 수 없도록 설정하려고합니다.div가 이미 삭제 된 경우 드롭을 사용 중지 하시겠습니까?

내가 살펴본 : 드롭을 비활성화

`greedy: true` 

하지만 난 다시 또한 그것을 가능하게하는 방법을 잘 모르겠어요 :

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

내가 얻을 수있는 이들 모두 해제하려면 블록/이미지가 원래 위치로 되돌아 가거나 다른 사각형으로 옮겨지면 다시 활성화 할 수있는 방법을 모르겠다.

전체 버전 : http://creativelabel.co.uk/drag-and-drop/

UPDATE : 이는 드롭 가능한 슬롯에 대한 코드입니다.

for (var i=0; i<=19; i++) { 
    var images = 'images/slot' + slotNumbers[i] + '.jpg'; 
$('<div class="placeholder"><div class="img-slot"></div></div>').attr('id', 'slot'+slotNumbers[i]).data('slotNumbers', slotNumbers[i]).appendTo('#imgSlots').droppable({ 
     accept: '#images img', 
     hoverClass: 'hovered', 
     drop: handleDropEvent, 
     activate: handleDragEvent 
}); 

drop: 이벤트 코드 :

function handleDropEvent(event, ui) { 
    var slotNumber = $(this).data('slotNumbers'); 
    var imgNumber = ui.draggable.data('number'); 
    ui.draggable.addClass('dropped'); 
    $(this).droppable('disable'); 
    ui.draggable.position({ of: $(this), my: 'right top', at: 'right top' }); 

    if (slotNumber == imgNumber) { 
    ui.draggable.addClass('img-match'); 
    ui.draggable.data("valid", true); 

    imgMatch++; 
    } else { 
     if(ui.draggable.data("valid")) { 
     imgMatch--; 
     ui.draggable.data("valid", false); 
     } 
    } 

activate: 코드 :

function handleDragEvent(event, ui) { 
    $(this).droppable('enable'); 
    if(ui.draggable.data("valid")) { 
     imgMatch--; 
     $('input[name=Score]').val(imgMatch); 
     $('#score h1').text(imgMatch); 
     ui.draggable.data("valid", false); 
    } 
} 

답변

5

docs 그건 당신이 사용하지 않도록 표시 ("방법"탭에서 봐) :

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

그리고 당신은

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

업데이트를 사용할 수 있도록 :이 라이브 예 확인 : 큰 상자에 빨간색 사각형의 http://jsfiddle.net/QqJRs/ 드래그를하고 드롭 (그것 떨어 표시하는 녹색으로 바뀝니다). 하나는 안에있는 동안 다른 사람을이 상자에 놓을 수 없습니다. 이제 드래그 아웃합니다 (빨간색으로 바뀌면 제거되었습니다). 이제 다른 사람들을 그 자리에 떨어 뜨릴 수 있습니다.

이 핵심은 아래의 내 의견에 설명 된대로입니다. 당신이 항목에 컨테이너를 연결 컨테이너에서 항목을 삭제할 때 : 다시 드래그 할 때, 그리고 unassociate

drop: function(event,ui){ 
    ui.draggable.addClass('dropped'); // taken from your code & I used to change color 

    ui.draggable.data('droppedin',$(this)); // associate the container 
    $(this).droppable('disable'); // disable the droppable 
} 

을하고 다시 활성화 :

는 는
drag: function(event,ui){ 
     if($(this).data('droppedin')){ 
      $(this).data('droppedin').droppable('enable'); v// re-enable 
      $(this).data('droppedin',null); // de-associate 
      $(this).removeClass('dropped') // remove class 
     } 
    } 
+0

음,보고 당신이 게시되지로 모든 코드 (그리고 아니, 귀하의 전체 코드베이스에서 귀하의 링크를 통해 선별하지 않음) 그것은 당신의 코드에 이것을 넣을 위치에 대해 언급하기가 약간 어렵습니다. 'droppable '을 활성화하고 관련 코드 섹션으로 질문을 업데이트해야한다고 생각하는 곳을 좁히려 고하십시오. – Jamiec

+0

좋아요. 사용 가능하다고 생각하는 부분에 대한 제 질문을 업데이트했습니다. 사용자가 광장을 다른 곳으로 드래그하여 활성화 코드를 삽입하는 논리적 인 장소처럼 보이는 경우 정품 인증 코드에서 점수를 제거합니다. – ShambalaG

+0

그리고 그걸 시도해 봤어? 어떻게 된 거예요? – Jamiec

관련 문제