views:

241

answers:

2

Hello

I'm using 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;
    });

Any ideas?

+1  A: 

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 onscroll="scrolltop();" >

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

Brandon Boone
Thanks. Let me give it a try.
NLV
Thank you. It is working great. But Scroll(0,0) is giving a jumpy and flickering behavior. Can't i call something like preventDefault() and cancel the event?
NLV
I don't think you can (someone can correct me if I'm wrong). I believe the event is only called when the scroll has started, not before it happens. That is why we are moving the scroll position back to (0,0). It already moved before we entered our event. Glad you found it useful though.
Brandon Boone
A: 

I know this is a little late in the game but 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");
    }
});
gurun8
Yep. This works. This is exactly what i did.
NLV