获取地图区域(html)的位置? [英] Get position of map area(html)?
问题描述
这可能吗?我试图找到元素与浏览器相关的x和y坐标。
var position = $(this).position();
x = position.left;
y = position.right;
不起作用。
是有什么办法可以做到这一点?
http://adamsaewitz.com/housing/
突出显示蓝色房间070
编辑更新的问题:由于您使用 < area>
这是一个不同的故事,并且从 coords
属性中获取更容易,如下所示:
var position = $(this).attr('coords')。split(',');
x = +位置[0] - 50;
y = +职位[1] - 170;
偏移量仅用于说明工具提示本身的硬编码宽度/高度。除上述之外,您还希望使用顶部
和剩余
而不是 margin-top
和 margin-left
。同样要考虑 #content
< div>
在页面中的位置,给它一个<$ c
#content {position> $ c $> relative
position in tooltip to sit in in,like this: :亲戚; }
然后...而不是 .after()
,请使用 .append()
,所以它被添加到里面。
对于原始问题:
对象 .position()
返回值为 top
和 left
属性...但是您要 .offset()
无论如何(它与文档相关,其中 .position()
偏移父母),所以它应该看起来像这样:
var position = $(this).offset(),
x = position.left,
y = position.top; //不对!
或者:
var position = $(this).offset();
var x = position.left;
var y = position.top;
...但没有一个 var
逗号分隔的语句,或每行上的 var
,你也在创建(或试图)全局变量,这会在IE中爆炸。
Is this possible? I'm trying to find the x and y coordinates of the element in relation to the browser.
var position = $(this).position();
x = position.left;
y = position.right;
Doesn't work.
Is there any way to do this?
http://adamsaewitz.com/housing/
highlight the blue room 070
Edit for updated question: Since you're using <area>
it's a different story, and fetching from the coords
attribute is much easier, like this:
var position = $(this).attr('coords').split(',');
x = +position[0] - 50;
y = +position[1] - 170;
The offsets are just to account for the hard-coded width/height of the tooltip itself. In addition to the above, you want to use top
and left
rather than margin-top
and margin-left
. Also to account for the #content
<div>
's position in the page, give it a relative
position for the tooltip to sit in, like this:
#content { position: relative; }
Then...instead of .after()
, use .append()
so it gets added inside that parent.
For original question:
The object .position()
returns has top
and left
properties...but you want .offset()
here anyway (it's relative to the document, where .position()
is relative to the offset parent), so it should look like this:
var position = $(this).offset(),
x = position.left,
y = position.top; //not right!
Or this:
var position = $(this).offset();
var x = position.left;
var y = position.top;
...but without a single var
comma-separated statement, or a var
on each line, you're also creating (or trying to) global variables, which will blow up in IE.
这篇关于获取地图区域(html)的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!