Prevent Page behind JQuery UI Dialog from Scrolling

I love jQuery UI dialogs. I hate when scrolling the dialogs’ content to the bottom starts scrolling the page underneath, moving the dialog off screen. With some smart structure of the page’s html, that can be avoided.

jquery-UI-dialogFirst, let’s show the problem. This is a basic page with a jQuery UI dialog shown. The dialog is modal, so it prevents access to the page behind. There are scroll bars on both the dialog and then page. Scrolling the dialog’s content with the mouse wheel works fine, until the bottom of the dialog is reached. At this point, I do expect continued rolling of the wheel to hit a dead end. Unfortunately it doesn’t. It starts scrolling the page behind the dialog instead. Rolling some more will eventually move the dialog off page, leaving the user with a large disabled area. That is definitely not a good user experience. Here is a small live demo.

The Body Scrolls

jquery-UI-scroll-badThe problem appears because the jQuery UI dialog is just a cleverly designed div that is added dynamically to the end of the body. Pressing F12 on the live demo page shows that there is a <div> right before the closing </body> tag. That div is the jQuery UI dialog.

When hitting the bottom of the dialog it can no longer be scrolled. Instead the parent element – the entire <body> is scrolled and the dialog moves off page. The solution is to no longer make the <body> scrollable but instead move the scrolling to a separate <div>.

Scroll a Div Instead

jquery-UI-scroll-goodInserting a separate <div> that handles the scrolling solves the problem. Here is a small live demo. Looking at the html in the developer tools of the browser shows that the jQuery UI dialog is still inserted at the bottom of the <body>, but now it’s no longer the <body> that scrolls but rather a separate wrapper <div>.

Scrolling Div Source

This is the source of the page that uses a separate <div> for scrolling. (Most of the Lorem Ipsum text block is removed for brevity.)

<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
        #wrapper
        {
            overflow-y: scroll;
            height: 100%;
        }
        #main
        {
            width: 800px;
            margin: auto;
        }
        html, body
        {
            margin: 0;
            overflow: hidden;
            height: 100%;
        }
    </style>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
    <link href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" />
    <script type="text/javascript">
        $(function () {
            $("<div><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p></div>")
            .dialog({ height: "200", modal: true });
        });
    </script>
    <title>Untitled Page</title>
</head>
<body>
    <div id="wrapper">
        <div id="main">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam a massa sed eros
            semper fringilla. Donec leo arcu, vehicula in egestas id, vulputate sit amet diam.
            ...
            ...            
        </div>
    </div>
</body>
</html>

One thought on “Prevent Page behind JQuery UI Dialog from Scrolling

Leave a Reply

Your email address will not be published.

Please do only post comments to the contents here. If you need help on the topic described in this post, please post a question at Stack Overflow instead (if you want you can link back to this post in your question). There is a much higher chance that you will get help quickly on Stack Overflow than here.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>