Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

prevent page scrolling when a modal is open

// When the modal is shown, we want a fixed body
document.body.style.position = 'fixed';
document.body.style.top = `-${window.scrollY}px`;

// When the modal is hidden...
const scrollY = document.body.style.top;
document.body.style.position = '';
document.body.style.top = '';
window.scrollTo(0, parseInt(scrollY || '0') * -1);
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #prevent #page #scrolling #modal #open
ADD COMMENT
Topic
Name
5+9 =