InterSectionObserver-粘性元素与fiewport+顶级物业

发布于 2025-01-27 00:26:32 字数 489 浏览 3 评论 0原文

我想在标头粘合时添加一堂课,但条件不应该与视口有关。它应该是视口和一些顶部(CSS)值。我的粘性组件是有一个顶级属性,可以从相对于视口粘的位置进行调整。

我尝试了许多事情,例如更改rootmargin,但没有成功,有些帮助将不胜感激

const stickyElm = document.querySelector('.automations-dashboard > div > span:nth-child(3)');
const observer = new IntersectionObserver(
  ([e]) => e.target.classList.toggle('isSticky', e.intersectionRatio < 1),
  { rootMargin: '0px 0px 0px 0px' }
);
if (stickyElm) observer.observe(stickyElm);

。 “>要检测位置何时触发的事件

I wanted to add a class when the header was sticky but with a condition that it should not be with respect to viewport. It should be viewport plus some top ( css ) value. My sticky component is having a top property to adjust from where it will sticky with respect to viewport.

I tried many things such as changing rootMargin but was unsuccessful, some help would be appreciated

const stickyElm = document.querySelector('.automations-dashboard > div > span:nth-child(3)');
const observer = new IntersectionObserver(
  ([e]) => e.target.classList.toggle('isSticky', e.intersectionRatio < 1),
  { rootMargin: '0px 0px 0px 0px' }
);
if (stickyElm) observer.observe(stickyElm);

Reference SO - Event to detect when position:sticky is triggered

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文