Html 滚动页面时强制 div 元素保持在同一位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18746569/
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 13:17:01  来源:igfitidea点击:

Force div element to stay in same place, when page is scrolled

csshtml

提问by soniccool

I created a divelement, that I placed all the way on the right of my site. The only problem is that its at the top of the site, so if i scroll down it remains there.

我创建了一个div元素,并将其一直放置在我网站的右侧。唯一的问题是它在网站的顶部,所以如果我向下滚动它仍然在那里。

How can I force it to remain in the same part of the page, when page is being scrolled?

滚动页面时,如何强制它保留在页面的同一部分?

This is, what I've managed to figure out myself so far:

这是,到目前为止我已经设法弄清楚自己:

<div style="width: 200px; background-color: #999; z-index: 10; position: absolute; right: 0; top: 0; height: 83px;">
</div>

回答by Anton D

Change position:absoluteto position:fixed;.

更改position:absoluteposition:fixed;

Example can be found in this jsFiddle.

示例可以在这个 jsFiddle 中找到。

回答by user2769260

Use position: fixedinstead of position: absolute.

使用position: fixed代替position: absolute

See here.

这里

回答by SOULAT TOQEER

You can do this replacing position:absolute;by position:fixed;.

您可以position:absolute;通过替换来执行此操作position:fixed;

回答by Debu Shinobi

There is something wrong with your code.

您的代码有问题。

position : absolutemakes the element on top irrespective of other elements in the same page. But the position not relative to the scroll

position : absolute无论同一页面中的其他元素如何,都将元素置于顶部。但位置不是相对于滚动

This can be solved with position : fixedThis property will make the element position fixed and still relative to the scroll.

这可以解决position : fixed此属性将使元素位置固定并仍然相对于滚动。

Or

或者

You can check it out Here

你可以在这里查看