2011-12-19 74 views
-1

我想知道,如果一個節點相交,這樣的架構中的另一個節點: schema如何知道一個節點是否與另一個節點相交?

+0

[兄弟元素重疊時高效檢測]的可能重複(http://stackoverflow.com/questions/1560926/efficiently-detect-when-sibling-elements-overlap) – 2011-12-19 22:20:08

+0

相關,可能重複:http://stackoverflow.com /問題/ 5720837 /如何對檢測元素重疊-使用重疊-JavaScript的 – 2011-12-19 22:22:28

回答

0

使用DOM屬性topleft屬性弄清楚它們與clientWidthclientHeight

這搗鼓重疊檢查一起在那裏(假設你移動這兩個框):http://jsfiddle.net/maniator/JnTaq/

代碼:

$('.drag').draggable({ 
    stop: function(event, ui) { 
     var others = $('.drag').not(this), 
      _self = this, 
      length = $(_self).width(); 
     others.each(function(i, item) { 
      var this_left = parseInt(item.style.left) - length, 
       this_top = parseInt(item.style.top) - length; 
      if (ui.position.left > this_left && ui.position.top > this_top) { 
       var left = Math.abs(ui.position.left - this_left), 
        top = Math.abs(ui.position.top - this_top); 
       if (left <= (length*2) && top <= (length*2)) { 
        alert('overlap'); 
       } 
      } 
     }); 
    } 
}); 
相關問題