To prevent body scrolling while a modal window is active in Webflow, follow these updated steps and use the appropriate method.
overflow property:
<script>
document.addEventListener('DOMContentLoaded', function () {
const openModal = document.querySelector('.open-modal');
const closeModal = document.querySelector('.close-modal');
const modal = document.querySelector('.modal'); // Change this selector based on your modal
const body = document.body;
openModal.addEventListener('click', function() {
modal.style.display = 'block';
body.style.overflow = 'hidden';
});
closeModal.addEventListener('click', function() {
modal.style.display = 'none';
body.style.overflow = 'auto';
});
});
</script>
.modal. Update the JavaScript code if your class name differs..open-modal and .close-modal.
.open-modal to see the modal and check that body scrolling is disabled..close-modal restores the body scrolling.
In your Webflow site, add the provided script under the Before </body> Tag section in Project Settings. It toggles the body’s overflow property to prevent scrolling when a modal is active and restores it upon closing the modal. Ensure all selectors in your script match those in your design for smooth operation.