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

Default

User image

Default

Name

  • Osmo Discount
    -25%
Osmo Basics/

Detect Scrolling Direction (Up/Down)

Detect Scrolling Direction (Up/Down)

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
<body data-scrolling-started="false" data-scrolling-direction="up"></body>

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 initDetectScrollingDirection() {
  let lastScrollTop = 0;
  const threshold = 10; // Minimal scroll distance to switch to up/down 
  const thresholdTop = 50; // Minimal scroll distance from top of window to start

  window.addEventListener('scroll', () => {
    const nowScrollTop = window.scrollY;

    if (Math.abs(lastScrollTop - nowScrollTop) >= threshold) {
      // Update Scroll Direction
      const direction = nowScrollTop > lastScrollTop ? 'down' : 'up';
      document.querySelectorAll('[data-scrolling-direction]').forEach(el => 
        el.setAttribute('data-scrolling-direction', direction)
      );

      // Update Scroll Started
      const started = nowScrollTop > thresholdTop;
      document.querySelectorAll('[data-scrolling-started]').forEach(el => 
        el.setAttribute('data-scrolling-started', started ? 'true' : 'false')
      );

      lastScrollTop = nowScrollTop;
    }
  });
}

// Initialize Detect Scrolling Direction
document.addEventListener('DOMContentLoaded', () => {
  initDetectScrollingDirection();
});

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-scrolling-started="false"] to the <body>, used to check if user scrolled
  • Add [data-scrolling-direction="up"] to the <body>, used to check if user scrolling up or down

Example structure

For both Webflow and Code

<body data-scrolling-started="false" data-scrolling-direction="up">
  <nav></nav>
</body>

Animating

Use CSS to animate objects based on the changing attributes

.nav {
  transition: transform 1s ease, padding 1s ease;
  transform: translateY(0%) rotate(0.001deg);
  padding: 2em 1em;
}

/* Shrink nav when scrolling started */
[data-scrolling-started="true"] .nav {
  padding: 1em 1em;
}

/* Move nav out of window when scrolling down */
[data-scrolling-started="true"][data-scrolling-direction="down"] .nav {
  transform: translateY(-100%) rotate(0.001deg);
}

/* Change background to filled when scrolling started */
.nav__inner {
  transition: background-color 1s ease;
  background-color: transparent;
}

[data-scrolling-started="true"] .nav__inner {
  background-color: #fff;
}

Resource Details

Scrolling
Detect
Setup
Script
Javascript
Up
Down

Original source

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.