Back to tutorials
User / Level Icon
Intermediate
Gear Time Icon
Webflow
Time Icon
10
 Minutes

Stop Page Scroll on Modal

Dereksiu.com.au - Derek Siu Headshot smiling

Taught by:

Derek Siu

In this ‪Webflow‬ tutorial, I'll answer a comment from my previous video: explaining how to create a basic popup in Webflow. The comment was on how to prevent the page from scrolling when the popup is opened.

<script>  document.addEventListener('DOMContentLoaded', function() {    const disableScrollButton = document.getElementById('disableScrollButton');    const modal = document.getElementById('myModal');    const modalCloseButton = document.getElementById('closeModalButton');    if (disableScrollButton) {      disableScrollButton.addEventListener('click', function(event) {        event.preventDefault();        event.stopPropagation();        document.body.style.overflow = 'hidden'; // Prevent scrolling        modal.style.display = 'flex'; // Show the modal      });    }    if (modalCloseButton) {      modalCloseButton.addEventListener('click', function(event) {        event.preventDefault();        event.stopPropagation();        document.body.style.overflow = 'auto'; // Enable scrolling        modal.style.display = 'none'; // Hide the modal      });    }    // Optional: Re-enable scrolling if modal is clicked outside    window.addEventListener('click', function(event) {      if (event.target == modal) {        document.body.style.overflow = 'auto'; // Enable scrolling        modal.style.display = 'none'; // Hide the modal      }    });  });</script>

Copy Code

The Ultimate Guide to Preventing Page Scrolling in Webflow

Webflow is a powerful web design tool that allows you to create stunning websites with ease. However, there are times when you may want to prevent the page from scrolling, such as when a modal popup is displayed. In this guide, we will show you how to prevent page scrolling in Webflow using custom JavaScript, ensuring a seamless user experience while maintaining compatibility with native Webflow interactions.

Why Prevent Page Scrolling?

Preventing page scrolling can enhance the user experience in several ways:

  • Focus on Content: It ensures that users remain focused on important content, such as modals or popups.
  • Improved Accessibility: Helps in preventing background content from becoming a distraction.
  • Enhanced Design Control: Gives you greater control over the design and functionality of your web elements.

Step-by-Step Guide to Prevent Page Scrolling in Webflow

1. Add a Custom Code Embed

To start, you need to add a custom code embed to your Webflow project. Here’s how:

  1. Open your Webflow project.
  2. Navigate to the page where you want to add this functionality.
  3. Drag an Embed element from the Add Elements panel onto the canvas.

2. Insert JavaScript Code

Inside the Embed element, add the following JavaScript code (mentioned above)

3. Save and Publish

Save your changes and publish your site to see the effect.

Best Practices for Using Modals

Using modals effectively can greatly improve the user experience. Here are some best practices:

  • Clear Purpose: Ensure that the modal has a clear purpose and provides value to the user.
  • Simple Design: Keep the design simple and intuitive.
  • Accessible: Make sure the modal is accessible to all users, including those using screen readers.
  • Easy to Close: Provide a clear and easy way to close the modal, such as a close button.

Conclusion

Preventing page scrolling in Webflow is a simple yet effective way to enhance user experience. By following the steps outlined in this guide, you can ensure that your modal popups are displayed correctly without interfering with native Webflow interactions. Remember to always test your changes thoroughly to ensure a smooth and seamless experience for your users.

For more tips and tricks on Webflow and web design, be sure to check out our other blog posts!

How Did You Find This Tutorial?
Ready to implement this tutorial?
Get started with Webflow Today!
Webflow Icon Logo
Signup For A Free Webflow Account
Stay updated with my Newsletter!
Have Any Further Questions?
Contact Me
Basic Linkedin Icon
Basic Pinterest Icon
Basiic Maill iicon