[英]How to make element stick on top when scroll
I am creating a page I need help with, I have the HTML and CSS ready, all I want is to make the element come to the top after I scroll down a bit, and there have to be more than 5 screens than I need to come to top as I keep scrolling我正在创建一个需要帮助的页面,我已经准备好 HTML 和 CSS,我想要的只是在我向下滚动一点后使元素到达顶部,并且必须有超过 5 个屏幕而不是我需要的当我继续滚动时来到顶部
I can't find a solution so need help我找不到解决方案,所以需要帮助
Here's a link of what I need, this is exactly what I want https://www.blackrock.com/corporate#intro这是我需要的链接,这正是我想要的 https://www.blackrock.com/corporate#intro
CSS CSS
The CSS property you need is position: sticky
means that this element would be in it's respective relative
position until you scroll down enough and it reached the top (if you set its top: 0
) and would then "stick" to the top as if it instantly changed it's position to position: fixed
.您需要的 CSS 属性是
position: sticky
意味着该元素将在其各自的relative
position 中,直到您向下滚动到足够的顶部并到达top: 0
,然后“如果您设置它”它立即将其 position 更改为position: fixed
。 Enjoy享受
The following code could help you achieve your desired behavior:以下代码可以帮助您实现所需的行为:
.sticky-container {
position: sticky;
top: 0;
left: 0;
}
If it is a container that takes up the entire width then also add width: 100%
and a certain fixed height in pixels to see the container.如果它是一个占据整个宽度的容器,那么还要添加
width: 100%
和某个固定的高度(以像素为单位)以查看容器。
If you want to have the element stay in a certain position in default when the user just entered the website you probably would need position: fixed
instead of sticky.如果您想让元素在用户刚进入网站时默认保留在某个 position 中,您可能需要
position: fixed
而不是粘性。 You could take a look at a similar solution for position fixed here with a demo您可以通过演示查看此处修复的 position 的类似解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.