Copied SVG to clipboard
Something went wrong
Copied code to clipboard
Something went wrong

Default

User image

Default

Name

  • Osmo Discount
    -25%
Osmo Basics/

Check Section Theme on Scroll

Check Section Theme on Scroll

Documentation

Webflow

Code

Setup: External Scripts

External Scripts in Webflow

Make sure to always put the External Scripts before the Javascript step of the resource.

In this video you learn where to put these in your Webflow project? Or how to include a paid GSAP Club plugin in your project?

HTML

Copy

Step 1: Copy structure to Webflow

Copy structure to Webflow

In the video below we described how you can copy + paste the structure of this resource to your Webflow project.

Copy to Webflow

Webflow structure is not required for this resource.

Step 1: Add HTML

HTML

Copy

HTML structure is not required for this resource.

Step 2: Add CSS

CSS

Copy

Step 2: Add custom Javascript

Custom Javascript in Webflow

In this video, Ilja gives you some guidance about using JavaScript in Webflow:

Step 2: Add Javascript

Step 3: Add Javascript

Javascript

Copy
function initCheckSectionThemeScroll() {

  // Get detection offset, in this case the navbar
  const navBarHeight = document.querySelector("[data-nav-bar-height]")
  const themeObserverOffset = navBarHeight ? navBarHeight.offsetHeight / 2 : 0;

  function checkThemeSection() {
    const themeSections = document.querySelectorAll("[data-theme-section]");

    themeSections.forEach(function(themeSection) {
      const rect = themeSection.getBoundingClientRect();
      const themeSectionTop = rect.top;
      const themeSectionBottom = rect.bottom;

      // If the offset is between the top & bottom of the current section
      if (themeSectionTop <= themeObserverOffset && themeSectionBottom >= themeObserverOffset) {
        // Check [data-theme-section]
        const themeSectionActive = themeSection.getAttribute("data-theme-section");
        document.querySelectorAll("[data-theme-nav]").forEach(function(elem) {
          if (elem.getAttribute("data-theme-nav") !== themeSectionActive) {
            elem.setAttribute("data-theme-nav", themeSectionActive);
          }
        });

        // Check [data-bg-section]
        const bgSectionActive = themeSection.getAttribute("data-bg-section");
        document.querySelectorAll("[data-bg-nav]").forEach(function(elem) {
          if (elem.getAttribute("data-bg-nav") !== bgSectionActive) {
            elem.setAttribute("data-bg-nav", bgSectionActive);
          }
        });
      }
    });
  }

  function startThemeCheck() {
    document.addEventListener("scroll", checkThemeSection);
  }

  // Initial check and start listening for scroll
  checkThemeSection();
  startThemeCheck();
}

// Initialize Check Section Theme on Scroll
document.addEventListener('DOMContentLoaded', () => {
  initCheckSectionThemeScroll();
});

Step 3: Add custom CSS

Step 2: Add custom CSS

Custom CSS in Webflow

Curious about where to put custom CSS in Webflow? Ilja explains it in the below video:

CSS

Copy

Implementation

Attributes

  • Add [data-theme-nav="light"] to the <body>, used to check the theme of the <section>
  • Add [data-bg-nav="light"] to the <body>, used to check the background of the <section>
  • Add [data-theme-section] to all of the <section> elements that need to be synced with the nav theme
  • Add [data-bg-section] to all of the <section> elements that need to be synced with the nav background

Example structure

For both Webflow and Code

<body data-theme-nav="light" data-bg-nav="light">
  <nav></nav>
  <section class="section-header" data-theme-section="dark" data-bg-section="blue"></section>
  <section class="section-work" data-theme-section="dark" data-bg-section="orange"></section>
  <section class="section-about" data-theme-section="light" data-bg-section="yellow"></section>
  <section class="section-contact" data-theme-section="light" data-bg-section="light"></section>
</body>

Animating

Use CSS to animate objects (or in this case the nav) based on the changing attributes

/* Nav Theme */
[data-theme-nav="light"] .nav__inner {
  color: #131313;
}

[data-theme-nav="dark"] .nav__inner {
  color: #EFEEEC;
}

/* Nav Background */
[data-bg-nav="pink"] .nav__inner {
  background-color: #562041;
}

[data-bg-nav="black"] .nav__inner {
  background-color: #1F1F1F;
}

[data-bg-nav="lightblue"] .nav__inner {
  background-color: #204b63;
}

[data-bg-nav="darkgreen"] .nav__inner {
  background-color: #C9FC7D;
}

Resource Details

Detect
Theme
Color
Sections
Script
Javascript
Basic
Navigation

Original source

Dennis Snellenberg

Creator Credits

We always strive to credit creators as accurately as possible. While similar concepts might appear online, we aim to provide proper and respectful attribution.