How to disable scrolling when modal is open - Full Page JS
P粉090087228
2023-08-18 00:15:20
<p>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. </p>
<pre class="brush:php;toolbar:false;"><div id="fullpage">
<div class="section">
<?php include './home-page/home-page-manufacturing-list.php';?>
<button id="turnOff" onclick="document.getElementById('id01').style.display='block'" class="w3-button w3-black">Open modal box</button>
</div>
</div>
<div id="id01" class="w3-modal">
<div class="w3-modal-content">
<div class="w3-container">
<span onclick="closeModal('modal01')" class="w3-button w3-display-topright">×</span>
<div class="container background-filter">
<div class="row">
<div class="col-12">
<h3 class="section-title"></h3>
</div>
<div class="col-12">
<h5>At our company, we have a passion for woodworking and it shows in all of our fabrication and interior design projects. We specialize in creating custom wood furniture, fixtures and accents that are as functional as they are beautiful</h5>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function() {
var isModalOpen = false;
// Disable FullPage.js scrolling when the modal is open
function disableFullPageScroll() {
$.fn.fullpage.setAllowScrolling(false);
$.fn.fullpage.setKeyboardScrolling(false);
}
// Enable FullPage.js scrolling when the modal is closed
function enableFullPageScroll() {
$.fn.fullpage.setAllowScrolling(true);
$.fn.fullpage.setKeyboardScrolling(true);
}
//Open modal box
function openModal(modalId) {
document.getElementById(modalId).style.display = 'block';
isModalOpen = true;
disableFullPageScroll();
}
// Close the modal box
function closeModal(modalId) {
document.getElementById(modalId).style.display = 'none';
isModalOpen = false;
enableFullPageScroll();
}
// Handle button click events to open and close the modal
$('#openModalButton').on('click', function() {
openModal('id01'); // Replace 'id01' with the ID of your modal
});
$('#closeModalButton').on('click', function() {
closeModal('id01'); // Replace 'id01' with the ID of your modal
});
// Handle scroll events
$(window).on('scroll', function(event) {
if (isModalOpen) {
event.preventDefault();
event.stopPropagation();
}
});
});
</script></pre>
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: