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

              
                  <!-- fixed navigation -->
    <nav>
      <ul>
        <li data-section="one" class="active">#1</li>
        <li data-section="two">#2</li>
        <li data-section="three">#3</li>
        <li data-section="four">#4</li>
        <li data-section="five">#5</li>
        <li data-section="six">#6</li>
      </ul>
    </nav>
    <!--END fixed navigation -->

    <!-- sections -->
    <section class="one">
      <div class="subsection">
        <h1 class="title">Intersection Observer</h1>
        <p class="text">
          The Intersection Observer API provides a way to asynchronously observe
          changes in the intersection of a target element with an ancestor
          element or with a top-level document's viewport.
        </p>
      </div>
      <div class="subsection">
        <img src="https://jssecrets.com/wp-content/uploads/2023/03/bubble.png" />
      </div>
    </section>
    <section class="two">
      <h5 class="text">
        Historically, detecting visibility of an element, or the relative
        visibility of two elements in relation to each other, has been a
        difficult task for which solutions have been unreliable and prone to
        causing the browser and the sites the user is accessing to become
        sluggish. As the web has matured, the need for this kind of information
        has grown. Intersection information is needed for many reasons, such as:
      </h5>
    </section>
    <section class="three">
      <div class="subsection">
        <img src="https://jssecrets.com/wp-content/uploads/2023/03/lazy.png" />
      </div>
      <div class="subsection">
        <img src="https://jssecrets.com/wp-content/uploads/2023/03/scroll.png" />
      </div>
    </section>
    <section class="four">
      <h5 class="text">
        Implementing intersection detection in the past involved event handlers
        and loops calling methods like Element.getBoundingClientRect() to build
        up the needed information for every element affected. Since all this
        code runs on the main thread, even one of these can cause performance
        problems. When a site is loaded with these tests, things can get
        downright ugly.
      </h5>
    </section>
    <section class="five">
      <div class="subsection">
        <p class="text">
          The Intersection Observer API lets code register a callback function
          that is executed whenever an element they wish to monitor enters or
          exits another element (or the viewport), or when the amount by which
          the two intersect changes by a requested amount.
        </p>
      </div>
      <div class="subsection">
        <div class="blob">
          <img src="https://jssecrets.com/wp-content/uploads/2023/03/shape.png" />
        </div>
      </div>
    </section>
    <section class="six">
      <h5 class="text">
        One thing the Intersection Observer API can't tell you: the exact number
        of pixels that overlap or specifically which ones they are; however, it
        covers the much more common use case of "If they intersect by somewhere
        around N%, I need to do something."
      </h5>
    </section>
    <!-- END sections -->

    <!-- made in jssecrets.com -->
    <a class="jssecrets" href="https://jssecrets.com/" target="_blank"
      >jssecrets.com</a
    >
    <!-- made in jssecrets.com -->
              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css2?family=Urbanist:wght@500&display=swap");
*,
::before,
::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}

ul {
  list-style: none;
}

@font-face {
  font-family: "Gosha Sans";
  src: url('https://ilyass.sg-host.com/PPGoshaSans-Regular.otf');
  font-weight: 400;
}
@font-face {
  font-family: "Gosha Sans";
  src: url('https://ilyass.sg-host.com/PPGoshaSans-Bold.otf');
  font-weight: 800;
}
* {
  font-family: "Gosha Sans";
}

nav {
  width: 120px;
  background: #fff;
  padding: 12px;
  border: 6px solid black;
  box-shadow: 1px 1px black, 2px 2px black, 3px 3px black, 4px 4px black, 5px 5px black, 6px 6px black, 7px 7px black, 8px 8px black;
  height: 320px;
  color: black;
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  right: 28px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 100;
}
@media (min-width: 1200px) {
  nav {
    width: 200px;
    padding: 24px;
    height: 400px;
  }
}
nav ul {
  width: 100%;
  text-align: center;
}
nav ul li {
  display: list-item;
  font-size: 16px;
  padding: 12px;
  transition: 0.4s;
  position: relative;
}
@media (min-width: 1200px) {
  nav ul li {
    font-size: 24px;
    padding: 16px;
  }
}
nav ul li.active {
  background-color: #ff7051;
  border: 4px solid #000;
  font-weight: 800;
  font-size: 24px;
}
@media (min-width: 1200px) {
  nav ul li.active {
    font-size: 40px;
    border: 6px solid #000;
  }
}

section {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 48px;
  transition: 2s;
  flex-direction: column;
}
@media (min-width: 1200px) {
  section {
    flex-direction: row;
  }
}
section .subsection {
  width: 100%;
}
@media (min-width: 1200px) {
  section .subsection {
    width: 50%;
  }
}
section img, section .blob {
  max-width: 200px;
}
@media (min-width: 1200px) {
  section img {
    max-width: 400px;
  }
}
section.one {
  background-color: #23a094;
  display: flex;
}
section.one .subsection {
  height: 100%;
}
section.one .subsection:nth-of-type(1) {
  background-color: #ffc900;
  border: 6px solid #000;
  border-right-width: 4px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding-left: 48px;
}
section.one .subsection:nth-of-type(1) .title {
  margin-bottom: 24px;
}
section.one .subsection:nth-of-type(1) .paragraph {
  max-width: 80%;
}
section.one .subsection:nth-of-type(2) {
  background-color: #23a094;
  border: 6px solid #000;
  border-left-width: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
}
section.one .subsection:nth-of-type(2) img {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
section.two {
  background-color: #ff90e8;
}
section.three {
  background-color: #f1f333;
}
section.three .subsection {
  height: 100%;
}
section.three .subsection:nth-of-type(1) {
  background-color: #b23386;
  border: 6px solid #000;
  border-right-width: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
}
section.three .subsection:nth-of-type(1) img {
  position: relative;
  top: -50px;
}
section.three .subsection:nth-of-type(2) {
  background-color: #ff7051;
  border: 6px solid #000;
  border-left-width: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
}
section.three .subsection:nth-of-type(2) img {
  position: relative;
  top: 50px;
}
section.four {
  background-color: #90a8ed;
}
section.five .subsection {
  height: 100%;
}
section.five .subsection:nth-of-type(1) {
  background-color: #23a094;
  border: 6px solid #000;
  border-right-width: 4px;
  display: flex;
  align-items: center;
  padding-left: 48px;
}
section.five .subsection:nth-of-type(1) .paragraph {
  max-width: 80%;
}
section.five .subsection:nth-of-type(2) {
  background-color: #f1f333;
  border: 6px solid #000;
  border-left-width: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
}
section.five .subsection:nth-of-type(2) .blob {
  width: 700px;
  height: 700px;
  position: relative;
}
section.five .subsection:nth-of-type(2) .blob svg path {
  fill: #ff90e8;
  stroke: #000;
  stroke-width: 3px;
}
section.five .subsection:nth-of-type(2) .blob img {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
section.six {
  background-color: #b23386;
}
section.red {
  background-color: rgb(233, 81, 81);
  color: white;
}

.title {
  font-size: 24px;
  font-weight: 800;
}
@media (min-width: 320px) {
  .title {
    font-size: 40px;
  }
}
@media (min-width: 575px) {
  .title {
    font-size: 60px;
  }
}
@media (min-width: 1200px) {
  .title {
    font-size: 92px;
  }
}

.text {
  max-width: 80%;
  font-weight: 400;
  overflow-wrap: break-word;
  word-wrap: break-word;
  -ms-hyphens: auto;
  -moz-hyphens: auto;
  -webkit-hyphens: auto;
  hyphens: auto;
  font-size: 16px;
}
@media (min-width: 320px) {
  .text {
    font-size: 20px;
  }
}
@media (min-width: 575px) {
  .text {
    font-size: 28px;
  }
}
@media (min-width: 1200px) {
  .text {
    font-size: 48px;
  }
}

.jssecrets {
  font-weight: 500;
  text-decoration: none !important;
  font-family: "Urbanist";
  padding: 16px;
  background: #ffc805;
  color: #000000;
  font-size: 1.2em;
  letter-spacing: 0.2px;
  border-radius: 8px;
  box-shadow: 0px 4px 12px rgba(184, 144, 0, 0.1882352941), 0px 8px 24px rgba(184, 144, 0, 0.3764705882);
  position: absolute;
  bottom: 24px;
  right: 24px;
  transition: all 0.3s;
}
.jssecrets:hover {
  transform: translateY(-4px);
}
              
            
!

JS

              
                // variables
const sections = document.querySelectorAll('section');
const navElements = document.querySelectorAll('ul li');
let activeSectionIndicator = '';

// intersection observer
const observer = new IntersectionObserver(
  (entries) => {
    entries.forEach((entry) => {
      if (entry.isIntersecting) {
        document.querySelector(`ul li.active`).classList.remove('active');
        activeSectionIndicator = entry.target.classList[0];
        document
          .querySelector(`ul li[data-section="${activeSectionIndicator}"]`)
          .classList.add('active');
      }
    });
  },
  { 
    root: document, /* <-- This is necessary for an iframe to properly target itself as the root document instead of the parent window. https://github.com/w3c/IntersectionObserver/issues/372 */
    rootMargin: '-10% 0px -90% 0px' 
  }
  
  
);

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

              
            
!
999px

Console