What I want to do is, when the modal is open, disable JavaScript scrolling for the entire page. The problem is that when I open the modal and try to scroll, it does move the content behind the modal, which is the actual web page, and I want to disable that. When the modal is opened, the background should be frozen.
The code you provided seems correct to disable scrolling when the modal is opened. However, there are several possible causes of the problem:
openModal
function you defined in the script. Instead, it directly manipulates the modal's style. This means that theisModalOpen
variable is not set totrue
and thedisableFullPageScroll
function is not called. To solve this problem you should use theopenModal
function when the button is clicked:closeModal
function. It should look like this:×
closeModal
The function is not defined in the global scope, but it is called from HTML. This may cause errors. To solve this problem, you should define thecloseModal
function in the global scope:The
Please try these suggestions and let me know if they solve your problem.disableFullPageScroll
andenableFullPageScroll
functions use FullPage.js methods to disable and enable scrolling. If you are not using FullPage.js, or it is not initialized correctly, these methods will not work. You should check that FullPage.js is included and initialized correctly in your project.The
overflowpreventDefault
andstopPropagation
methods in a scroll event handler may not be sufficient to prevent scrolling in all cases. You may also want to set thestyle to
hiddenwhen the modal is open, and reset it to
auto when the modal is closed: