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>
  • Chris on 2013-06-17

    Try this (from http://stackoverflow.com/questions/4887332/):

    $(formObject).dialog({
     create: function(event, ui) {
      $("body").css({ overflow: 'hidden' })
     },
     beforeClose: function(event, ui) {
      $("body").css({ overflow: 'inherit' })
     }
    });

  • Joe DF on 2014-04-27

    Wow! nice trick! thanks a lot Mr Anders Abel!
    It works way better than the “overflow: hidden” trick and doesn’t even require JavaScript.
    It’s simply brilliant!

    Cheers, joedf

  • Luis on 2014-07-14

    Excellent, works perfectly and it was very easy to implement in Oracle Apex. Thanks.

  • Danil on 2014-07-16

    There is a problem in this method.
    When you refresh the page, scroll position is not saved.

  • Leave a Reply

    Your name as it will be displayed on the posted comment.
    Your e-mail address will not be published. It is only used if I want to get in touch during comment moderation.
    Your name will be a link to this address.
Software Development is a Job – Coding is a Passion

I'm Anders Abel, a systems architect and developer working for Kentor in Stockholm, Sweden.

profile for Anders Abel at Stack Overflow, Q&A for professional and enthusiast programmers

The complete code for all posts is available on GitHub.

Popular Posts

Archives

Series

Powered by WordPress with the Passion for Coding theme.