How to detect when an element over another element in JavaScript?

后端 未结 3 1611
生来不讨喜
生来不讨喜 2021-02-09 01:26

I wrote most of the code... And when the element is \"fully\" over the other element it works. The problem is that I don\'t just want it to be true when the element is \"fully\"

3条回答
  •  悲&欢浪女
    2021-02-09 01:38

    Standard video game method:

    doElsCollide = function(el1, el2) {
        el1.offsetBottom = el1.offsetTop + el1.offsetHeight;
        el1.offsetRight = el1.offsetLeft + el1.offsetWidth;
        el2.offsetBottom = el2.offsetTop + el2.offsetHeight;
        el2.offsetRight = el2.offsetLeft + el2.offsetWidth;
    
        return !((el1.offsetBottom < el2.offsetTop) ||
                 (el1.offsetTop > el2.offsetBottom) ||
                 (el1.offsetRight < el2.offsetLeft) ||
                 (el1.offsetLeft > el2.offsetRight))
    };
    

    See demo

提交回复
热议问题