2014-02-14 2 views
0

그래서 나는 드래그 가능한 객체가 있습니다 - 정지 위치가 특정 범위 (div 이상)에 있는지 어떻게 테스트 할 수 있습니까?드래그 스톱이 특정 div에 있는지 확인하십시오.

내가 할 수있는 최선의 방법은 정확한 위치를 찾는 것입니다. 감사.

바이올린 http://jsfiddle.net/f5n66/

$(init); 

function init() { 
    $('.draggable').draggable({ 
    opacity:0.7, helper:"clone", 
    stop: function(event, ui) { 
    if(ui.position === $('#resultArea').position()){ 
     console.log(ui.position); 
     } 
} 
    }); 

}

당신은 강하가 DROPZONE의 DIV의 크기 안에 있는지 확인해야

답변

1

.

function init() { 
    $('.draggable').draggable({ 
    opacity:0.7, helper:"clone", 

    stop: function(event, ui) { 
     var coords = $('#resultArea').position(); 
     coords.bottom = coords.top + $('#resultArea').height(); 
     coords.bottomRight = coords.left + $('#resultArea').width(); 
     if(ui.position.top >= coords.top && ui.position.top <= coords.bottom && ui.position.left >= coords.left && ui.position.left <= coords.bottomRight){ 
      console.info("inside"); 
     }else{ 
      console.info("outside"); 
     } 
    } 
     }); 
} 

Live Demo

업데이트 : 찾는 개별 함수의 계산에 재사용 할

function inDropZone(drag, drop){ 
    var coords = drop.position(); 
    coords.bottom = coords.top + drop.height(); 
    coords.bottomRight = coords.left + drop.width(); 
    if(drag.position.top >= coords.top && drag.position.top <= coords.bottom && drag.position.left >= coords.left && drag.position.left <= coords.bottomRight){ 
     return true; 
    }else{ 
     return false; 
    } 
}