1

我正在使用 elementFromPoint 来查找在被 ui 掩码捕获后将鼠标事件传递给哪个元素。我在掩码处捕获事件,隐藏掩码,然后使用 elementFromPoint(event.pageX, event.pageY) 获取底层元素。

问题是 elementFromPoint 似乎根本不适用于绝对定位的元素。它只是给出了第一个非绝对定位的父级。有没有办法同时获得绝对定位和非绝对定位的元素,或者一旦我获得带有 elementFromPoint 的父级,我是否只需要手动搜索绝对定位的子级?

谢谢

4

2 回答 2

1

My experience is regular old document.elementFromPoint(...) works for absolutely positioned elements too, but it's sensitive to the "order" of the elements (which element is really "in front"). I've found that sometimes to get it to work the way I want, I have to explicitly specfy the CSS "z-index" (sometimes with a positive value of 10 or more) on the absolutely positioned element.

This is especially an issue if the elements have no background and contain just text, as all of them will be visible at the same time in the same place. From just the appearance, it can be hard to tell which one is really "in front", perhaps not the one you think.

于 2013-08-09T22:58:44.133 回答
0

我尝试并创建了代码,它的工作非常好看看

html

<div id="one">1</div>
<div id="two">2</div>
<div id="three">3</div>
<div id="four">4</div>
<div id="five">5</div>
<div id="six">6</div>

css

div {
    width: 50px;
    height: 50px;
    background-color: black;
    color: white;
    margin-bottom: 30px;
}
#five{
    position: absolute;
    left: 10px;
    top: 20px;
    background: red;
}

js

$(document).ready(function(){        
    $(document.body).bind('click', function(e){
        var elem = document.elementFromPoint(e.pageX, e.pageY);

        alert('you selected element id: ' + elem.id);
    });
});
于 2013-07-17T21:09:55.900 回答