简体   繁体   English

通过绝对坐标定位DOM元素

[英]Locating DOM element by absolute coordinates

有没有一种简单的方法来定位所有“覆盖”(即在其边界内)具有X / Y坐标对的像素的DOM元素?

You can have a look at document.elementFromPoint though I don't know which browsers support it. 您可以查看document.elementFromPoint但我不知道哪些浏览器支持它。
Firefox and Chrome do. Firefox和Chrome一样。 It is also in the MSDN , but I am not so familiar with this documentation so I don't know in which IE version it is included. 它也在MSDN中 ,但我不太熟悉这个文档,所以我不知道它包含在哪个IE版本中。

Update: 更新:

To find all elements that are somehow at this position, you could make the assumption that also all elements of the parent are at this position. 要查找以某种方式处于此位置的所有元素,您可以假设父项的所有元素都在此位置。 Of course this does not work with absolute positioned elements. 当然,这不适用于绝对定位元素。

elementFromPoint will only give you the most front element. elementFromPoint只会为您提供最前面的元素。 To really find the others you would have to set the display of the front most element to none and then run the function again. 要真正找到其他元素,您必须将最前面元素的display设置为none ,然后再次运行该函数。 But the user would probably notice this. 但是用户可能会注意到这一点。 You'd have to try. 你必须尝试。

I couldn't stop myself to jump on Felix Kling's answer: 我无法阻止自己跳过Felix Kling的回答:

var $info = $('<div>', {
    css: {    
        position:    'fixed',
        top:         '0px',
        left:        '0px',
        opacity:     0.77,
        width:       '200px',
        height:      '200px',
        backgroundColor: '#B4DA55',
        border:      '2px solid black'
    }
}).prependTo(document.body);

$(window).bind('mousemove', function(e) {
    var ele = document.elementFromPoint(e.pageX, e.pageY);
    ele && $info.html('NodeType: ' + ele.nodeType + '<br>nodeName: ' + ele.nodeName + '<br>Content: ' + ele.textContent.slice(0,20));
});

updated : background-color ! 更新 :背景色!

This does the job ( fiddle ): 这样做( 小提琴 ):

$(document).click(function(e) {
    var hitElements = getHitElements(e);
});

var getHitElements = function(e) {
    var x = e.pageX;
    var y = e.pageY;
    var hitElements = [];

    $(':visible').each(function() {
        var offset = $(this).offset();
        if (offset.left < x && (offset.left + $(this).outerWidth() > x) && (offset.top < y && (offset.top + $(this).outerHeight() > y))) {
            hitElements.push($(this));
        }
    });

    return hitElements;
}​

When using :visible , you should be aware of this: 使用时:visible ,你应该知道这个:

Elements with visibility: hidden or opacity: 0 are considered visible, since they still consume space in the layout. 具有可见性的元素:隐藏或不透明度:0被认为是可见的,因为它们仍然占用布局中的空间。 During animations that hide an element, the element is considered to be visible until the end of the animation. 在隐藏元素的动画期间,该元素在动画结束前被视为可见。 During animations to show an element, the element is considered to be visible at the start at the animation. 在显示元素的动画期间,该元素在动画开始时被视为可见。

So, based on your need, you would want to exclude the visibility:hidden and opacity:0 elements. 因此,根据您的需要,您可能希望排除visibility:hiddenopacity:0元素。

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

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