Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="page">
  <nav>
    <a href="#s1">Block 1</a>
    <a href="#s2">Block 2</a>
    <a href="#s3">Block 3</a>
    <a href="#s4">Block 4</a>
    <a href="#footer">Footer</a>
  </nav>
  <main>
    <section id="s1" style="height:70px">
       Block 1
    </section>
    <section id="s2" style="height:250px">
       Block 2
    </section>
    <section id="s3" style="height:85px">
       Block 3
    </section>
    <section id="s4" style="height:500px">
       Block 4
    </section>
    <section id="footer" style="height:1500px">
       Footer
    </section>
  </main>
</div>
              
            
!

CSS

              
                html {
  scroll-behavior: smooth;
}
main {
  padding-top: 50px;
}
section {
  position:relative;
  scroll-margin: 50px;
  padding: 20px;
  &:nth-child(2n) {
    background-color: orange;
  }
}
nav {
  z-index: 99;
  position: fixed;
  background: lightgreen;
  width: 100%;
  left:0;
  top:0;
  padding: 15px;
  height: 50px;
  box-sizing: border-box;
  a {
    &.active {
      background: yellow;
    }
  }
}
              
            
!

JS

              
                // callback function for the intersection observer.
const changeNav = (entries, observer) => {
  
  let updated = false; // whether the menu was already updated during this intersection.
  
	entries.forEach((entry) => {
    
    // if the menu was aleady updated by a previous entry, we don't need to check the other entries.
    if (updated) {
      return;
    }
    
    let activeBlock; // the block that should be highlighted in the menu.
		
    if(!entry.isIntersecting) {
      
      // This element has just left the viewport. This means at least one of 2 possibilities:
      // 1. the next element has its top aligned with the top of the viewport.
      //    In this case the next element should be activated.
      // 2. the previous element is (at least partly) visible.
      //    In this case we need to keep looking at previous elements until
      //    we find the highest one that is still visible.
      
      if (isInViewport(entry.target.nextElementSibling)) {
        
        // possibility 1.
        activeBlock = entry.target.nextElementSibling;
        
      } else if (isInViewport(entry.target.previousElementSibling)) {
        
        // possibility 2.
        activeBlock = entry.target.previousElementSibling;
        while ( isInViewport(activeBlock.previousElementSibling) ) {
          activeBlock = activeBlock.previousElementSibling;
        }
        
      }
      
    } else {
      
      // This element just entered the viewport. 2 possibilities:
      // 1. The top of the element moved in from the bottom.
      //    In this case nothing needs to happen.
      // 2. The bottom of the element moved in from the top.
      //    In this case the current element should also become active.
      
      if (!isInViewport(entry.target.nextElementSibling)) {
        
        // possibility 1.
        return;
        
      } else if (!isInViewport(entry.target.previousElementSibling)) {
        
        // possibility 2.
        activeBlock = entry.target;
        
      }
    }
    
    // remove active class on all elements.
    document.querySelectorAll('nav > a.active').forEach((aEl) => {
      aEl.classList.remove('active');
    });
    
    // Couldn't deternmine an active block. So don't do anything.
    if (!activeBlock) {
      return;
    }

    // get id of the intersecting section
    const id = activeBlock.getAttribute('id');
    
    // find matching link and add appropriate class
	  document.querySelector(`[href="#${id}"]`).classList.add('active');
    
    // Wer're done. Set flag to make sure that we don't check other entries.
    updated = true;
    
	});
}

function isInViewport(element) {
    if (!element) {
      return false;
    }
    const rect = element.getBoundingClientRect();
    return (
        rect.bottom >= 0 &&
        rect.top <= (window.innerHeight || document.documentElement.clientHeight)
    );
}

// init the observer
const options = {
  rootMargin: "-51px", // 1 pixel more than nav height
  root: document,
  treshhold: [],
}

const observer = new IntersectionObserver(changeNav, options);

// target the elements to be observed.
const sections = document.querySelectorAll('section');
sections.forEach((sectionNavTrigger) => {
	observer.observe(sectionNavTrigger);
});
              
            
!
999px

Console