Webflow sync, pageviews & more.
NEW

How can I implement smooth scrolling using Locomotive.js in Webflow while ensuring position sticky functionality is working correctly?

TL;DR
  • Wrap all main content in a div with the attribute data-scroll-container, and place sticky elements inside it.
  • Include Locomotive Scroll's CSS in the <head> and JS before </body>, then initialize it with a script targeting the scroll container.
  • Ensure sticky elements aren’t nested within elements using transform, set top in CSS, and disable Webflow’s native smooth scroll.

To implement smooth scrolling with Locomotive.js in Webflow and keep position: sticky working correctly, you need to properly configure both Locomotive Scroll and your CSS. Here's how to do it:

1. Prepare Your Webflow Project

  • Wrap your entire content (except for elements like navbars that should not be scrolled) inside a custom <div> wrapper. Commonly, this is called something like #smooth-wrapper or #js-scroll.
  • Give this wrapper a unique ID, for example, data-scroll-container. This is required for Locomotive.js to know what element to control.

2. Add Required Custom Attributes

  • On the scrolling container (e.g. your main wrapper), add a custom attribute:
  • Attribute: data-scroll-container
  • Value: leave it blank
  • For sticky elements, ensure they are children of the scroll container.

3. Embed Locomotive.js Scripts

  • Before closing </head>, include the Locomotive Scroll CSS using an Embed:
  • URL: Use a CDN like https://cdn.jsdelivr.net/npm/locomotive-scroll@4.1.4/dist/locomotive-scroll.min.css
  • Before closing </body>, include:
  • Locomotive Scroll JavaScript from a CDN like https://cdn.jsdelivr.net/npm/locomotive-scroll@4.1.4/dist/locomotive-scroll.min.js
  • A custom script to initialize it in Webflow.

4. Initialize Locomotive Scroll

  • Place this script in a Custom Code Embed just before the closing </body>:

    ```

    ```

  • Optional: Reinitialize scroll when using Webflow interactions or content loads dynamically.

  • Example: After finishing Webflow’s IX2 animations, you can re-call scroll.update().

5. Ensure position: sticky Works

  • Locomotive.js uses transform on the scroll container, which breaks position: sticky by default.
  • Solution: Set position: sticky only on elements that are inside the scroll container and not affected by transform.
  • Avoid placing sticky elements inside an element with transform. Stickiness will fail because of how position: sticky behaves inside transformed parents.

6. CSS Requirements

  • Do not apply transform, perspective, or overflow to parent containers of sticky elements unless necessary.
  • Sticky elements need a top value to specify how far from the top they should stick (e.g., top: 0;).

7. Optional Webflow Settings

  • Disable Webflow's native smooth scroll (used for anchor links):
  • In Page Settings, scroll to the bottom and disable “Smooth Scroll”. Locomotive handles this already.

Summary

To use Locomotive.js smooth scrolling with sticky elements in Webflow, wrap content in a data-scroll-container div, initialize Locomotive Scroll properly, and avoid applying transform to any parent of sticky elements. Place sticky elements inside the scroll container with top defined, and ensure Webflow's smooth scroll is disabled.

Rate this answer

Other Webflow Questions