[英]jQuery.offset() sets position: absolute. I need position: fixed
When you set an element's offset with jQuery.offset({coords})
it also sets the CSS property position
to absolute
. 当您使用
jQuery.offset({coords})
设置元素的偏移量时,它还将CSS属性position
为absolute
。
I have a div, however, that I set to position: fixed
in my CSS, and I want it to remain that way, even after setting the offset of the element with jQuery. 然而,我有一个div,我设置了
position: fixed
在我的CSS中,我希望它保持这种方式,即使在使用jQuery设置元素的偏移量之后。
Now, I'm sure I can probably set the offset, then set position: fixed
again, but I was wondering if there is a way I can tell jQuery to set the position to fixed instead of absolute when it sets offset. 现在,我确定我可以设置偏移量,然后设置
position: fixed
再次position: fixed
,但我想知道是否有一种方法可以告诉jQuery在设置偏移量时将位置设置为固定而不是绝对。
HTML
HTML
<div class="searchResults">
...
</div>
CSS
CSS
DIV.searchResults {
position: fixed;
padding: 20px;
background-color: red;
z-index: 501;
}
jQuery
jQuery的
$("DIV.searchResults").offset({left: 0, top: 0});
Rendered HTML
呈现HTML
<div class="searchResults" style="position: absolute; top: 0px; left: 0px;">
...
</div>
Obviously, since jQuery is setting the position in the style, it will trump the value of my CSS class. 显然,由于jQuery在样式中设置位置,它将胜过我的CSS类的值。 So I need a way to tell jQuery to set
position
to fixed
instead of absolute
, or tell it to set the offset without setting the value of the CSS property position
. 所以我需要一种方法来告诉jQuery将
position
设置为fixed
而不是absolute
,或者告诉它设置偏移量而不设置CSS属性position
的值。
As I commented above, in your case all you need is to modify the CSS for top and left like this: 正如我上面评论的那样,在您的情况下,您只需要修改顶部和左侧的CSS,如下所示:
$("DIV.searchResults").css({left: 0, top: 0});
Because the $.offset setter method only manipulates the left, top and position values to make the element relative to the page (from static to relative, and from fixed to absolute). 因为$ .offset setter方法只操纵left,top和position值来使元素相对于页面(从静态到相对,从固定到绝对)。 Since you want it position fixed, set the values directly.
由于您希望将其定位,请直接设置值。
Maybe not that elegant, but I think you can just chain .css()
after that to be sure that position
is set to fixed
like this: 也许不那么优雅,但我认为你可以在之后链接
.css()
以确保将position
设置为fixed
如下:
jquery jQuery的
$("DIV.searchResults").offset({
left: 0,
top: 0
}).css("position" : "fixed");
Not tested, but I think that'll work. 没有经过测试,但我认为这样可行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.