开发者

How do you make jQuery's .offset() work?

I'm working on a web app t开发者_高级运维hat I need to know the coordinates of an element relative to the web page. This seems like it should be simple with jQuery's offset(), however it is not working for me.

Here is the code I am using to find the offset:

 var offset=$('#container').offset();
 alert(offset.right);

Here is the code that I'm using for the whose position I want:

<div id='container' style='position:absolute; height:30px; width:300px; background-color:orange;'></div>

But when I load the page and the alert() fires, it alerts as undefined. Does anyone have any ideas on how I could fix this?


offset has top and left parameters not right

var offset=$('#container').offset();
alert(offset.left);


as a note, offset() displays the top and left relative to it's closest ancestor with position:relative (it defaults to the body tag, I think).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜