How can I freeze the body content while allowing the content in the Webflow lightbox to scroll separately?

TL;DR
  • Add a CSS rule in the Page Settings to prevent body scrolling by applying .lightbox-open { overflow: hidden; }.
  • Use JavaScript to toggle the .lightbox-open class on the body when the lightbox is opened and remove it when closed. 
  • Test by publishing the site and checking if the body remains frozen while the lightbox scrolls independently.

When using a Webflow lightbox, you may want to freeze the body content while allowing the lightbox content to scroll separately for better user experience. Here's how you can achieve that:

​

1. Add Custom CSS

  • Go to the Page Settings of the page where your lightbox is used.
  • Locate the "Custom Code" section at the bottom.
  • Add a <style> tag with a rule to prevent body scrolling when the lightbox is open:

  ```css

  .lightbox-open {

    overflow: hidden;

  }

  ```

​

2. Implement Custom JavaScript

  • Still in the Page Settings, locate the "Before </body> tag" section.
  • Add a script to dynamically apply the .lightbox-open class to the body when the lightbox is opened:

  ```javascript

  document.addEventListener('DOMContentLoaded', function() {

    const lightboxLinks = document.querySelectorAll('.w-lightbox');

    if (lightboxLinks.length) {

      lightboxLinks.forEach(link => {

        link.addEventListener('click', () => {

          document.body.classList.add('lightbox-open');

        });

      });

    }

    

    // Listen for when the lightbox closes

    window.addEventListener('click', (event) => {

      if (event.target.closest('.w-lightbox') === null) {

        document.body.classList.remove('lightbox-open');

      }

    });

  });

  ```

​

3. Test the Implementation

  • Publish your Webflow site to view the changes.
  • Open the lightbox and verify that the body content is frozen and that the lightbox content scrolls independently.
  • Close the lightbox and ensure that body scrolling is restored.

​

Summary

By adding minimal CSS and custom JavaScript, you can freeze the body content while allowing the Webflow lightbox content to scroll. Adding a CSS rule .lightbox-open to control overflow and a JavaScript event listener to manage class toggling achieves this separation of scrolling behaviors effectively.

Rate this answer

Other Webflow Questions