如何知道节点是否与另一个节点相交?

时间:2011-12-19 22:17:56

标签: javascript jquery

我想知道某个节点是否与此架构相交的另一个节点: schema

1 个答案:

答案 0 :(得分: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');
                }
            }
        });
    }
});