简体   繁体   English

JavaScript:检测 DOM 元素是否与 DOM 元素重叠、在内部或外部?

[英]JavaScript: Detect if DOM element overlaping, inside or ouside with DOM element?

I'm making a small game in JavaScript.我正在用 JavaScript 制作一个小游戏。 I've created a small example js fiddle demo link .我创建了一个小示例js 小提琴演示链接 There are three situations as listed bellow:有以下三种情况:

  • A: Outside of target object. A:在目标物体之外。
  • B: Overlaping with target object's border. B:与目标对象的边界重叠。
  • C: Inside of target object. C:目标物体内部。

According to Have object detect if completely inside other object JavaScript , I've known how to detect if the object inside of target (Situation C).根据有对象检测是否完全在其他对象 JavaScript 中,我知道如何检测对象是否在目标内部(情况 C)。 How about situation A and B?情况A和B怎么样?

<div class="main">
    <div class="target"></div>
    <div class="obj">A</div>
    <div style="top:15%; left:50%;" class="obj">B</div>
    <div style="top:25%; left:35%;" class="obj">C</div>
</div>

One way to do this would be to use Element#getBoundingClientRect() to obtain the absolute coordinates of the DOM elements being classified for overlap, containment, etc.一种方法是使用Element#getBoundingClientRect()来获取被分类为重叠、包含等的 DOM 元素的绝对坐标。

This function returns to top , right , bottom and left coordiates of the corresponding DOM element which you can use to determine containment of an element with respect to a container .此函数返回相应 DOM 元素的toprightbottomleft坐标,您可以使用它们来确定element相对于container

You could implement a function like findContainment() shown below, where the containment of element is classified against a container element:您可以实现如下所示的findContainment()类的函数,其中elementcontainer根据container元素进行分类:

 function findContainment(element, container) { /* Obtain the bounding rectangle for each element */ const brE = element.getBoundingClientRect() const brC = container.getBoundingClientRect() /* If the boundaries of container pass through the boundaries of element then classifiy this as an overlap */ if ( /* Does container left or right edge pass through element? */ (brE.left < brC.left && brE.right > brC.left) || (brE.left < brC.right && brE.right > brC.right) || /* Does container top or bottom edge pass through element? */ (brE.top < brC.top && brE.bottom > brC.top) || (brE.top < brC.bottom && brE.bottom > brC.bottom)) { return "overlap"; } /* If boundaries of element fully contained inside bounday of container, classify this as containment of element in container */ if ( brE.left >= brC.left && brE.top >= brC.top && brE.bottom <= brC.bottom && brE.right <= brC.right ) { return "contained" } /* Otherwise, the element is fully outside the container */ return "outside" } const main = document.querySelector(".main") console.log("A", findContainment(document.querySelector(".a"), main)) console.log("B", findContainment(document.querySelector(".b"), main)) console.log("C", findContainment(document.querySelector(".c"), main)) console.log("D", findContainment(document.querySelector(".d"), main)) console.log("E", findContainment(document.querySelector(".e"), main))
 .main { width: 50%; height: 200px; border: 5px solid #000; position: relative; } .obj { width: 40px; height: 40px; border: 1px solid blue; position: absolute; }
 <div class="main"> <div style="top:105%; left:25%;" class="obj a">A</div> <div style="top:15%; left:-5%;" class="obj b">B</div> <div style="top:20%; left:40%;" class="obj c">C</div> <div style="top:20%; left:110%;" class="obj d">D</div> <div style="top:90%; left:95%;" class="obj e">E</div> </div>

Here's a working fiddle as well - hope that helps!这也是一个工作小提琴- 希望有帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM