开发者

How to move an absolutely positioned <div> to 20px above a specific DOM event/element?

I'm using jQuery.

How to deal with 开发者_运维知识库it easily?


Try this code:

var offset = $(<dom-element>).offset();
$(<div>).css({ top: (offset.top - $(<div>).height() - 20) + "px",
               left: offset.left });

For more information, check out the CSS documentation of jQuery.

Also, for most events you can call event.target to get the element generating event.

To get current mouse position use event.pageX and event.pageY. Note that the event must come from a mouse action: onmousemove, onmouseup, onmousedown, onclick, etc...


use offset();

var offset = $('#elem1').offset(); $('#elem2').css('top', offset.top );

http://docs.jquery.com/CSS/offset

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜