简体   繁体   English

如何检测一个元素坐在另一个元素之上

[英]How to detect one element sitting on top of another

I have this situation: 我有这种情况:

在此输入图像描述

I need to detect situation when element sitting on top of another. 当元素坐在另一个元素之上时,我需要检测情况。 This is SVG elements: 这是SVG元素:

<circle r="210.56" fill="#1ABCDB" id="01" priority="4" cx="658" cy="386"></circle>
<circle r="210.56" fill="#1ADC4A" id="02" priority="4" cx="858" cy="386"></circle>

I have to clue how to do this, maybe someone can give me some hint. 我必须弄清楚如何做到这一点,也许有人可以给我一些提示。 I use jQuery. 我使用jQuery。 Much thx for help. 很多请求帮助。

Well, at least if you're dealing with circles, it's a bit simple, because circles have a nice property: for circles to overlap in any way, the sum of their radii should be greater than the distance between their two centers . 好吧,至少如果你正在处理圈子,这有点简单,因为圈子有一个很好的属性: 对于圆圈以任何方式重叠,它们的半径之和应该大于它们两个中心之间的距离

So it should just be a matter of: 所以它应该只是一个问题:

  1. figuring out the distance between the two center points (easy enough to get the centers, as they're defined in the <circle> element, and 找出两个中心点之间的距离(很容易得到中心,因为它们在<circle>元素中定义,并且
  2. add the two radius values (again, on the <circle> element), then 然后添加两个半径值(再次,在<circle>元素上)
  3. just compare. 只是比较。 If the radius sum is greater than the distance, then you've got an overlap . 如果半径和大于距离,则表示您有重叠

http://jsfiddle.net/sZ9N9/ http://jsfiddle.net/sZ9N9/

You could look at this function: 你可以看看这个功能:

http://jsfiddle.net/a9bnh/ http://jsfiddle.net/a9bnh/

function upperElements(el) {
    var top = el.offsetTop,
        left = el.offsetLeft,
        width = el.offsetWidth,
        height = el.offsetHeight,
        elemTL = document.elementFromPoint(left, top),
        elemTR = document.elementFromPoint(left + width - 1, top),
        elemBL = document.elementFromPoint(left, top + height - 1),
        elemBR = document.elementFromPoint(left + width - 1, top + height - 1),
        elemCENTER = document.elementFromPoint(parseInt(left + (width / 2)), parseInt(top + (height / 2))),
        elemsUpper = [];
    if (elemTL != el) elemsUpper.push(elemTL);
    if (elemTR != el && $.inArray(elemTR, elemsUpper) === -1) elemsUpper.push(elemTR);
    if (elemBL != el && $.inArray(elemBL, elemsUpper) === -1) elemsUpper.push(elemBL);
    if (elemBR != el && $.inArray(elemBR, elemsUpper) === -1) elemsUpper.push(elemBR);
    if (elemCENTER != el && $.inArray(elemCENTER, elemsUpper) === -1) elemsUpper.push(elemCENTER);
    return elemsUpper;
};

Richard's answer is good if there are 2 circles but if there are many then I would suggest the following steps 理查德的回答是好的,如果有2个圈子,但如果有很多,那么我会建议以下步骤

  1. Calculating the area covered by the overlapping circles 计算重叠圆圈所覆盖的区域
  2. Calculating the sum of areas of all the circles independently(since this is SVG and the radii of the circles are known, this would be easy to do) 独立计算所有圆的面积之和(因为这是SVG,圆的半径是已知的,这很容易做到)
  3. Comparing the 2 areas 比较2个区域
  4. If the 2 areas are the same, then there is no overlapping otherwise at least 2 circles overlap. 如果2个区域相同,则没有重叠,否则至少2个圆圈重叠。

The tricky step is step 1 which can be approximiately calculated using pixel painting algorithm(my preference). 棘手的步骤是步骤1,可以使用像素绘制算法(我的偏好)近似计算。 For other methods, you can go through the following stackoverflow question 对于其他方法,您可以查看以下stackoverflow问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测元素是否在另一个元素之上 - How to detect if the element is on top of another element 如何检测元素是否被另一个元素覆盖/重叠? - How to detect if an element is covered/overlapped by another one? 如何在Angular 2中为一个元素在另一个元素上设置动画? - How to animate one element on top of another element in Angular 2? 如何防止一个元素的子元素位于另一个元素之上 - How to prevent child from one element to be on top of another element 如何动态地将一个元素置于另一个元素之上? - How do you dynamically position one element on top of another? 如何在给定点直接或通过另一个元素的腔检测哪个元素可见/在顶部? - How to detect which element is visible/on-top directly or through the cavities of another element at a given point? 如果坐在另一个元素下面,则不应发生jQuery随机动画 - Jquery random animation shouldn't happen if sitting underneath another element 当事件设置为外部元素时,如何检测另一个元素内的元素? - How can I detect the element inside another element while the event is set to the outer one? 如何获取一个元素的变化高度值并将其作为 CSS “顶部”值返回 jQuery 中的另一个元素? - How to get a changing height value of one element and return it as a CSS "top" value on another element in jQuery? 如何从显示在另一个画布元素上方的一个画布元素中删除 - how to remove one canvas element from displaying on top of another canvas element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM