简体   繁体   English

位置:固定为浮动div

[英]position:fixed for a floated div

I have a div that is floated left in a wrapper container. 我有一个div,该div浮动在包装容器中。 Now I want the div to stay in one spot when the page is scrolled. 现在,当页面滚动时,我希望div停留在一个位置。 So I tried position:fixed, but this ruins the other css properties of the div. 所以我尝试了position:fixed,但是这破坏了div的其他CSS属性。

I've tried using javascript aswell: 我也尝试过使用javascript:

$(window).scroll(function () { 
    $("#id").css({'top',$(window).scrollTop() + 'px'});
});

That javascript isn't doing anything. 那个javascript没有做任何事情。 Is there any other way to do this, I still want to keep the float:left property intact. 还有其他方法可以执行此操作,我仍然想保持float:left属性不变。

Thanks 谢谢

See all the elements with different position attributes form different layers. 查看具有不同位置属性的所有元素形成不同的层。 So you have to adjust other elements when you keep this element 'position:fixed' 因此,当您保持此元素“ position:fixed”时,您必须调整其他元素

You have to keep empty space for this div forever, and it will stay fixed there even when you scroll your page. 您必须永远为该div保留空白空间,即使滚动页面,它也将保持固定在那里。

See this link for better understanding 请参阅此链接以获得更好的理解

http://davidwalsh.name/demo/css-fixed-position.php http://davidwalsh.name/demo/css-fixed-position.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM