Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI dialog - Outer window scrolls while scrolling inside the dialog

I'm using a jQuery UI dialog to show a popup containing a page. When I scroll inside the popup and if the scroll bars comes to the bottom, the parent page starts scrolling. How can I restrict the parent page from scrolling while scrolling inside the dialog?

I've created a modal dialog using the following code.

// Dialog
$('#dialog').dialog({
    autoOpen: false,
    width: 800,
    height: 550,
    minHeight: 500,
    maxHeight: 800,
    minWidth: 500,
    maxWidth: 900,
    modal: true,
    buttons: {
        "Cancel": function () {
            $(this).dialog("close");
        }
    }
});

$('#AddNewItems').click(function () {
    var currentURL = getURLOfCurrentPage();
    $('#dialog').dialog('open');
    $("#dialog").dialog("option", "width", 800);
    $("#dialog").dialog("option", "height", 550);
    $("#dialog").dialog( "option", "draggable", false );
    $("#dialog").dialog( "option", "modal", true );
    $("#dialog").dialog( "option", "resizable", false );
    $('#dialog').dialog("option", "title", 'My Title');
    $("#modalIframeId").attr("src", "http://myUrl");
    return false;
});
like image 409
NLV Avatar asked Aug 03 '10 08:08

NLV


3 Answers

This is what I use:

$('#dialog').dialog({
    autoOpen: false,
    width: 800,
    height: 550,
    minHeight: 500,
    maxHeight: 800,
    minWidth: 500,
    maxWidth: 900,
    modal: true,
    buttons: {
        "Cancel": function () {
            $(this).dialog("close");
        }
    },
    open: function(){
        $("body").css("overflow", "hidden");
    },
    close: function(){
        $("body").css("overflow", "auto");
    }
});
like image 73
gurun8 Avatar answered Nov 10 '22 03:11

gurun8


Something like this might work (this is untested):

<script type="text/javascript" language="Javascript">
    var stop_scroll = false;
    function scrolltop(){
        if(stop_scroll == true){
            scroll(0,0);
            // Or window.scrollTo(0,0);
        }
    }
</script>

In your body tag (<body></body>)

<body onscroll="scrolltop();" >

Last, set stop_scroll to true when you open the dialogue and false when you close it.

like image 20
Brandon Boone Avatar answered Nov 10 '22 05:11

Brandon Boone


gurun8's solution worked best for me. However, I needed a way to do this globally. My application is using dialogs on multiple pages, and I didn't want update all instances.

The following code will handle the opening and closing of all dialogs:

$('body').on('dialogopen', '.ui-dialog-content', function () {
    var $dialog = $(this);
    var $body = $('body');
    var height = $body.height();

    // Hide the main scrollbar while the dialog is visible. This will prevent the main window
    // from scrolling if the user reaches the end of the dialog's scrollbar.
    $body.css('overflow', 'hidden');

    // Calling resize on the dialog so that the overlay is resized for the scrollbars that are now hidden.
    $dialog.resize().on('dialogclose', function () {
        // Show the main scrollbars and unbind the close event handler, as if the
        // dialog is shown again, we don't want the handler to fire multiple times.
        $body.css('overflow', 'auto');
        $dialog.off('dialogclose');
    });
});

I am using jQuery v1.8.23. I tested this in Internet Explorer 8, Internet Explorer 9, Firefox 17 and Chrome 19.

like image 1
Garry English Avatar answered Nov 10 '22 05:11

Garry English