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>
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.
Preventing page scrolling can enhance the user experience in several ways:
To start, you need to add a custom code embed to your Webflow project. Here’s how:
Inside the Embed element, add the following JavaScript code (mentioned above)
Save your changes and publish your site to see the effect.
Using modals effectively can greatly improve the user experience. Here are some best practices:
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!