检测div的位置 [英] Detecting the position of a div

查看:66
本文介绍了检测div的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小游戏,其中一些方块围绕主要div移动.鼠标还保留一个div(我已将光标的位置分配给了该div).

I have created a small game, in which some blocks are move around a main div. The mouse which also holding a div (I had assigned the position of cursor to that div).

我只想找出光标div是否在块上移动(这些也是div).如果这样,那么游戏将结束.

I just want to trace out, whether the cursor div is moving over the blocks(those are also div). If it comes like that then the game will be over.

如何检测块或光标div是否彼此移动?

How can I detect whether the block or cursor div moves over each other?

推荐答案

如果您使用的是jQuery,则可以通过以下方法找到div的左侧,顶部,宽度和高度:

If you're using jQuery, you can find the left, top, width and height of a div by using these:

$(myDiv).offset().left
$(myDiv).offset().top
myDiv.offsetWidth
myDiv.offsetHeight

使用它们来计算每个div的左,右,上和下.然后,如果满足以下条件,则两个div相互重叠:

Use those to work out the left, right, top and bottom of each div. Then two divs overlap each other if:

left1 < right2 && left2 < right1 && top1 < bottom2 && top2 < bottom1

这篇关于检测div的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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