0

I have one sticky navbar where a list item highlights when its corresponding section is viewed via scroll and I am using IntersectionObserver. I am getting problem only for the first item which has its section defined immediately after the sticky navbar following the second section. The issue is when I scroll up from second section, I see isIntersecting:false (as expected) but the first section which I just entered is not showing any intersection although it shows isIntersecting:false as well at certain point when I continue scroll up.

Here is the JS code. The below createMenuList(ele) is called when a DOM section is loaded on the page. There are total 3 items/sections.

var elements = [], elementsLength = 3;
    function createMenuList(ele) {
      if (ele != null) {
          elements.push(ele);
      }
      else if (ele == null) {
          console.log('Sticky nav list is not matching with the page structure');
      }
    
      if (elements.length == elementsLength) {
          const config = { threshold: [0.5] };
          var observer = new IntersectionObserver(function (entries) {
              console.log(entries);
              entries.forEach(entry => {
                  if (entry.isIntersecting) {
                      var current = document.querySelector(".anchor .anchor-module li.active");
                      if (current) {
                          current.className = current.className.replace("active", "");
                          current.className = current.className.replace(" active", "");
                      }
                      entry.target.closest('#main_skip').querySelector('.anchor .anchor-module li > a[href="#' + entry.target.id + '"]').closest('li').className += " active";
                  }
              });
          }, config);
        elements.forEach(link => observer.observe(link));
    }

Thanks in advance for the help!

  • 1
    Could you create a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) for us to check out? – Emiel Zuurbier Feb 18 '21 at 19:15
  • @EmielZuurbier can you check it now? I have added the maximum code I could. Thanks for looking into it. – Naina kalia Feb 19 '21 at 03:45
  • It's still very hard to tell without being able to run your code. Read through the article I referred to. It has some instructions about how to create runnable code here in your quesetion. – Emiel Zuurbier Feb 19 '21 at 09:19
  • Did you ever figure this out? Similar thing is happening to me, I have 3 sections, with an observer for each, they all do the right thing on scroll down, but on scroll up the first section observer doesn't seem to work. – kyija Nov 23 '21 at 20:17
  • Also encountered this as of the moment, any lead? – GeraldTDPI Feb 22 '22 at 02:10

0 Answers0