Html 滚动时如何使div不移动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17918614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:36:17  来源:igfitidea点击:

How to make a div not move when scrolling?

htmlscroll

提问by user2629376

I have created this div on the top left of my website, which contains the logo. However, I want it to stay there and not move up and down when I am scrolling. Please advise.

我在我网站的左上角创建了这个 div,其中包含徽标。但是,当我滚动时,我希望它留在那里而不是上下移动。请指教。

<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

谢谢

回答by Natesan

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

for ref: http://www.w3schools.com/css/css_positioning.asp

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

回答by Zaheer Ahmed

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;">

回答by PJM257

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/