Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS Sticky #109

Open
sedationh opened this issue Nov 16, 2024 · 0 comments
Open

CSS Sticky #109

sedationh opened this issue Nov 16, 2024 · 0 comments

Comments

@sedationh
Copy link
Owner

sedationh commented Nov 16, 2024

最近修了个 Bug, 用到了 Sticky 这个 CSS 特性,予以记录

https://developer.mozilla.org/en-US/docs/Web/CSS/position?v=example#sticky_positioning

Note

sticky 是一种强大的 CSS 特性,它结合了 relativefixed 的定位行为:

  • 元素在滚动区域内表现为相对定位 (relative)。
  • 当滚动达到设置的阈值时,元素会固定在视口的某个位置 (fixed)。

https://codepen.io/sedationh/pen/poMBNQz

<iframe src="https://codepen.io/sedationh/pen/poMBNQz" style="width: 100%; height: 500px; border: none;"></iframe>

在上面的例子中, containter 的高度为 300px, 其子元素 list 为 500px 结合 overflow: auto; 就产生了滚动

这里重点提一下, containter 上我特意设置了 50px, 如果你在 stikcy 的元素上设置 top: 0; 则是下面的效果

image

想要吸到顶部,需要设置 top: -50px; 才可以

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant