简体   繁体   English

滚动时如何使div不移动?

[英]How to make a div not move when scrolling?

I have created this div on the top left of my website, which contains the logo. 我在我网站的左上角创建了这个div,其中包含徽标。 However, I want it to stay there and not move up and down when I am scrolling. 但是,我想让它留在那里,而不是在我滚动时上下移动。 Please advise. 请指教。

<div style="padding: 5px 0 0 5px; height: 140px; width: 150px;">
   <p align="left">
      <img src="images/logo.png" border="5" alt="Logo" />
   </p>
</div>

Thanks 谢谢

<div style="padding: 5px 0 0 5px; height: 140px; width: 150px;position:fixed;left:0;top:0">

对于参考: http//www.w3schools.com/css/css_positioning.asp

position:fixed; 位置是:固定; An element with fixed position is positioned relative to the browser window. 具有固定位置的元素相对于浏览器窗口定位。 It will not move even if the window is scrolled. 即使滚动窗口,它也不会移动。

so change your css: 所以改变你的CSS:

<div style="position : fixed; padding: 5px 0 0 5px; height: 140px; width: 150px;">

Add an id to your div like this: 像这样在你的div中添加一个id:

<div style="padding: 5px 0 0 5px; height: 140px; width: 
150px;"id="idOfDiv">
<p align="left"><img src="images/logo.png" border="5" alt="Logo" />
</p>
</div>

Add to CSS: 添加到CSS:

#idOfDiv {position:fixed;}

Now, it should not scroll with the rest of the page or the parent element. 现在,它不应该与页面的其余部分或父元素一起滚动。

Example in jsfiddle: https://jsfiddle.net/PJM257/pL1s6jpz/ jsfiddle中的示例: https ://jsfiddle.net/PJM257/pL1s6jpz/

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

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