2014-02-14 3 views
0

Итак, у меня есть объект с перетаскиванием. Как я могу проверить, находится ли его положение остановки в определенном диапазоне (над div)Найти, если drag stop находится в определенном div?

Лучше всего я могу найти его точное положение. Благодарю.

Fiddle 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); 
     } 
} 
    }); 

}

ответ

1

Вы должны проверить, если падение внутри размерности Dropzone дел.

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

Update: Место вычисления в отдельной функции, чтобы сделать его многоразовым

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; 
    } 
}