简体   繁体   English

根据重叠的部分切换汉堡菜单背景

[英]toggle a burger menu background based on which section is overlapping

I am trying to change the burger menu on my website everytime it sits on top of a section with a light background for visibility. 我试图每次在网站上的汉堡菜单位于浅色背景下以提高可见性时都更改其汉堡菜单。 I tried to use intersection observer, and the solution seems working, however it triggers too early on scroll, before actually the section intersects with the menu. 我尝试使用相交观察器,该解决方案似乎可以正常工作,但是在该部分实际上与菜单相交之前,它在滚动时触发得太早。

I am new to intersection observer, I tried to find something useful online but with no much luck. 我是路口观察员的新手,我试图在网上找到一些有用的东西,但是运气不佳。

    const body = document.querySelector('body')
    const sections = body.querySelectorAll('.change-menu')
    const options = {
        root: null,
        threshold: 0,
        rootMargin:'-40px 0px -80% 0px'
    }


    const observer = new IntersectionObserver((entries,observer) => {
            entries.forEach(entry => {
                if(entry.isIntersecting) {
                document.querySelectorAll('.menu-item').forEach(bar => {
                    body.classList.add('black')
                })
            } else {
                document.querySelectorAll('.menu-item').forEach(bar => {
                    body.classList.remove('black')
                })
            }
            })

    }, options)



    sections.forEach(section => {
      observer.observe(section)
    })

Here is a jsfiddle of an example of my problem: 这是我的问题的一个例子:

https://jsfiddle.net/bc7w8zt1/ https://jsfiddle.net/bc7w8zt1/

Anyone can shed some light on it please? 任何人都可以对它有所了解吗?

I think the JSFiddle is buggy, at least, I cannot get any response from it when I change the IO settings. 我认为JSFiddle至少存在错误,至少在更改IO设置时无法从中得到任何响应。 On a real site, having a fixed top margin and variable bottom margin is viewport-height dependent: the top margin must be higher up in the viewport than the bottom margin, otherwise the CB doesn't fire. 在实际站点上,具有固定的顶部边距和可变的底部边距取决于视口高度:视口中的顶部边距必须高于底部边距,否则CB不会触发。 I think this is part of the issue in JSFiddle - the viewport is very short. 我认为这是JSFiddle问题的一部分-视口非常短。 For instance, with rootMargin:'-50px 0px -90% 0px' the viewport (window.innerHeight) must be at least 500px (actually ~490px worked) so that the div you are observing triggers the viewport's bottom margin at least 50px (10% of innerHeight) down from the top BEFORE it hits the top margin a fixed 50px from the top. 例如,使用rootMargin:'-50px 0px -90% 0px' ,视口(window.innerHeight)必须至少为500px(实际上〜490px起作用),以便您观察的div触发视口的底边距至少为50px(10在到达顶部页边距之前固定50px,然后再从顶部向下移动。 If the bottom is inside (that is, above) the top it won't fire. 如果底部在顶部内(即上方),则不会开火。 For any viewport, though again I couldn't get anything from JSFiddle, this seemed to work: rootMargin:'0px 0px -100% 0px' . 对于任何视口,尽管我再也无法从JSFiddle得到任何东西,但这似乎可行: rootMargin:'0px 0px -100% 0px' This will not trigger the IO callback until the grey div's top reaches the top of the viewport, and as you keep scrolling the grey div will be intersecting until it is off-screen. 在灰色div的顶部到达视口的顶部之前,这不会触发IO回调,并且随着您不断滚动,灰色div将相交直到屏幕外。 The hamburger menu class changes accordingly. 汉堡菜单类别会相应更改。

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

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