is it possible to disable scrolling in browser (just for browser's scrollbars) while a jQuery UI modal dialog is open.
Note: I do want scrolling to be enabled inside the dialog
Approach: A simple solution to this problem is to set the value of the “overflow” property of the body element to “hidden” whenever the modal is opened, which disables the scroll on the selected element.
To hide the scrollbar and disable scrolling, we can use the CSS overflow property. This property determines what to do with content that extends beyond the boundaries of its container. To prevent scrolling with this property, just apply the rule overflow: hidden to the body (for the entire page) or a container element.
$(formObject).dialog({ create: function(event, ui) { $("body").css({ overflow: 'hidden' }) }, beforeClose: function(event, ui) { $("body").css({ overflow: 'inherit' }) } });
Or as I allude to in the comment below:
var dialogActiveClassName="dialog-active"; var dialogContainerSelector="body"; $(formObject).dialog({ create: function(event, ui) { $(dialogContainerSelector).addClass(dialogActiveClassName); }, beforeClose: function(event, ui) { $(dialogContainerSelector).removeClass(dialogActiveClassName); } });
But actually to be honest, you should ensure that creating a dialog bubbles an event up to your window object where you'd be watching for said events, roughly something like this pseudo:
var dialogActiveClassName="dialog-active"; var dialogContainerSelector="body"; $(window).on("event:dialog-opened", function(){ $(dialogContainerSelector).addClass(dialogActiveClassName); }); $(window).on("event:dialog-closed", function(){ $(dialogContainerSelector).removeClass(dialogActiveClassName); });
I needed to do exactly the same thing, do it simply by adding a class to the body:
.stop-scrolling { height: 100%; overflow: hidden; }
Add the class then remove when you want to re-enable scrolling, tested in IE, FF, Safari and Chrome.
$('body').addClass('stop-scrolling')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With