查找(视觉上)堆叠在jquery中的元素之下的元素 [英] find elements that are stacked under (visually) an element in jquery

查看:64
本文介绍了查找(视觉上)堆叠在jquery中的元素之下的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有2个div(未分配z索引),则在上方分层,我可以使用对顶部div的引用来查找位于其下方的div吗?

if i have 2 divs (z index is not assigned), one layered over the over, can i use the reference to the top div to find which div is below it?

就DOM结构而言,这两个div是同级的.但从视觉上看,它们彼此堆叠.

as far as the DOM structure goes, these two divs would be siblings. but visually they are stacked on one another.

这是一个例子:

<body>
<div id="box1" style="background:#e0e0e0;height:100px;width:100px;"></div>
<div id="box2" style="background:#000000;height:100px;width:100px;margin-top:-50px;"></div>
</body>

这将导致以下结果:

which results in this:

因此,我试图找出具有黑色div box2的方法(使用选择器)在jquery中返回box1的方法,因为box1在jquery的box2下面.

so i'm trying to figure out, when having the black div, box2, a way to return box1 in jquery (using selectors), because box1 is beneath box2, using jquery.

推荐答案

检查box1是否与页面上的某个位置共享相同的位置.

Check if box1 is sharing the same position as a certain spot on the page.

而且只是因为我有点无聊,所以我才做的很棒

And only because I'm a little bored, I made this awesome-er

http://jsfiddle.net/hunter/PBAb6/

function GetAllElementsAt(x, y) {
    var $elements = $("body *").map(function() {
        var $this = $(this);
        var offset = $this.offset();
        var l = offset.left;
        var t = offset.top;
        var h = $this.height();
        var w = $this.width();

        var maxx = l + w;
        var maxy = t + h;

        return (y <= maxy && y >= t) && (x <= maxx && x >= l) ? $this : null;
    });

    return $elements;
}

这篇关于查找(视觉上)堆叠在jquery中的元素之下的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆