Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable browser scrolling while jQuery UI modal dialog is open

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

like image 240
Omu Avatar asked Feb 03 '11 14:02

Omu


People also ask

How do I disable scrolling when modal dialog is open?

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.

How do you stop background scrolling in CSS?

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.


2 Answers

$(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); }); 
like image 86
airtonix Avatar answered Sep 21 '22 05:09

airtonix


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') 
like image 25
hallodom Avatar answered Sep 21 '22 05:09

hallodom