JQuery 将一个元素设置在另一个元素的中间
嗨,我有一些像这样的元素:
<div id="one">content</div>
<div id="two">content</div>
和相应数量的元素(没有任何父元素,它们就在 body 标签之后),它们具有:
position: absolute;
并且具有这样的 id:
id="helper-one" refers to id="one"
现在我想将第二组元素恰好放置在引用元素的中间(垂直和水平),我该怎么做?
我尝试过使用 offset:
var one_offset = $("#one").offset();
$("#helper-one").offset({ top: one_offset.top, right: one_offset.right, left:one_offset.left, bottom: one_offset.bottom})
但它仅设置顶部和左侧的位置,将助手定位在元素的左上角而不是其中心
Hi I have some elements like these:
<div id="one">content</div>
<div id="two">content</div>
and a corrispondent number of elements (without any parent, they are just after the body tag) that have:
position: absolute;
and with an id like that:
id="helper-one" refers to id="one"
Now i want to place the second group of elements exactly at the middle (vertical and horizontal) of referred elements, how can i do that?
I've tried with offset:
var one_offset = $("#one").offset();
$("#helper-one").offset({ top: one_offset.top, right: one_offset.right, left:one_offset.left, bottom: one_offset.bottom})
but it set position only for top and left positionating the helper at the top-left corner of the element and not at its center
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用为
$("#helper-one").center($("#one"));
ps:你甚至可以通过解析原始元素的 id 来跳过 obj 参数
Call as
$("#helper-one").center($("#one"));
ps: you may even skip obj argument by parsing the id of the original element