简体   繁体   English

导航栏不适用于 Intersection Observer API

[英]Navigation bar doesn't work with Intersection Observer API

I have a problem when I use the Intersection Observer API.我在使用 Intersection Observer API 时遇到问题。 I am trying to use it for, once the nav-bar is not visible anymore when you scroll, make this latter appears with a white background and fixed to the viewport.我正在尝试使用它,一旦滚动时导航栏不再可见,使后者以白色背景显示并固定到视口。

I first of all tried to console.log('visible') and console.log('visible') when the nav-bar is visible or not and I succeed, But when I wanted to apply the new class when the navbar wasn't visible anymore: my page starting to get mad, the class was applied and removed, and the console was only displaying "visible".我首先尝试在导航栏可见或不可见时使用console.log('visible')和console.log('visible')并且我成功了,但是当我想应用新的class时导航栏不是' t 不再可见:我的页面开始变得疯狂,class 被应用和删除,控制台只显示“可见”。 "not visible" all the time very frequently. “不可见”一直非常频繁。

I think it is because when I apply the class, it moves the rootMargin (of the options object) but I don't know how to fix it.我认为这是因为当我应用 class 时,它会移动(选项对象的)rootMargin,但我不知道如何修复它。

My entire website (my code is in app.js): https://replit.com/@Xeway/IntersectionObserver#app.js我的整个网站(我的代码在 app.js 中): https://replit.com/@Xeway/IntersectionObserver#app.js

PS: I only have build app.js, HTML and CSS are codes made by FreeCodeCamp. PS:我只有 build app.js,HTML 和 CSS 是 FreeCodeCamp 制作的代码。 PS: Sorry for the link, but I can't share this code here because my code uses backtick and it doesn't seems to work ^^ Also, try to open the website with a big width because it's responsive and you can see more precisely the problem when it's on a computer's screen width. PS:抱歉链接,但我不能在这里分享这段代码,因为我的代码使用了反引号,它似乎不起作用^^另外,尝试用大宽度打开网站,因为它是响应式的,你可以看到更多当它在计算机的屏幕宽度上时,正是问题所在。

Thank you in advance for your answers guys:)提前感谢您的回答,伙计们:)

Intersection observer is not needed for your case.您的情况不需要交叉口观察员。 You just need to apply class to your header, when window is scrolled beyond certain height.当 window 滚动超过一定高度时,您只需要将 class 应用于您的 header。 In your case, it would be equal to the height of the header.在您的情况下,它将等于 header 的高度。 So, when the window scroll height > height of the header, then apply class and in the else, remove the class.因此,当 window 滚动高度 > header 的高度时,然后应用 class 并在其他情况下删除 ZA2F2ED4A298EBC2ABCB1DDC24。 Call the same function on document.ready as well (To check the window scroll position on the page load, if the window is already scrolled). Call the same function on document.ready as well (To check the window scroll position on the page load, if the window is already scrolled).

Here is the little script for your help:这是为您提供帮助的小脚本:

$(window).scroll(function() {
    var scroll = $(window).scrollTop();
    // assume 100px is the height of the header
    if (scroll >= 100) {
        addScrolledClassToHeader();
    } else {
        removeScrolledClassToHeader();
    }
});

function addScrolledClassToHeader() {
    $('header.site-header').addClass('header-is-scrolled');
}

function removeScrolledClassToHeader() {
    $('header.site-header').removeClass('header-is-scrolled');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM