iandevlin.com

Icon

A mixture of musings on web design & development, history, and life

Shadowbox – preventing the window underneath from scrolling

shadowbox

I’m a big fan of the web-based media viewer application Shadowbox and use it where necessary. However in a recent project I noticed that the contents of the window over which the shadowbox overlay opens, remains scrollable using the mouse wheel.

I wanted to prevent this behaviour and as usual I performed a Google search. I found many questions on it, but no answers. So I looked into it myself and managed to come up with something that works and is quite easy really.

You simply take advantage of the onOpen and OnClose options within Shadowbox and assign a function to each of them:

Shadowbox.init({
   language: 'en',
   players: ['html', 'iframe'],
   modal: true,
   onOpen: onShOpen,
   onClose: onShClose
});
function onShOpen() {
    document.body.style.overflow = "hidden";
    return true;
}
function onShClose() {
    document.body.style.overflow = "auto";
    return true;
}

As you can see, when the shadowbox opens, the function onShOpen is called, and this sets the body overflow to be hidden, and thus non-scrollable. The function onShClose undoes this.

Simples!

7 Responses

  1. Nice solution. I’m curious, with so many “lightbox” type applications out there, what made you decide to go with Shadowbox?

  2. ian says:

    Hi Michael. I went with Shadowbox because most of the other lightboxes are for images only. Shadowbox allows you to put almost anything within a lightbox.

    For example, in the code above, I’ve specified that the content will be HTML or an iFrame.

  3. Jesper says:

    Thanks a lot! This is a great ‘hack’.

    Unfortunately I cannot get it to work in IE 7 and 8 (bummer…). The underlying window is still scrollable, and when the shadowbox window is closed it seems like the script has messed up scrolling on that particular page.

    My skills in javascripting are not good, hence my question: Does anybody know of a ‘fix’ for this IE-specific problem?

    Otherwise the script works great in Firefox, Safari, Chrome etc.

    Thanks in advance!

  4. Jesper says:

    OK, by disabling ‘compatibility mode’ in IE 8 the script will run just fine. It seems the problem lies in how IE 7 and below does (not) interpret the “overflow” property. Oh dear!

  5. Ian says:

    Hi Jesper, hmm that’s interesting, and definitely needs a bit more looking into. If you find out anything else, ideally a fix, please post it here and I’ll update the article.

    Hopefully I’ll have a chance to look at it myself some time.

    Actually, how about adding position:relative to the body? This tends to fix issues with overflow on IE6 and 7, but I’m not sure if applying it to the body has any effect?

  6. Jesper says:

    Hi Ian!

    Adding position:relative did help a lot! Thank you very much!

    It now works almost perfect in IE 7 (and 8 in comp. mode), and the page beneath won’t scroll when a shadowbox window is open. There’s only seems to be one minor issue left: The scrollbar remains “greyed out” when the shadowbox window is closed, BUT the page is still scrollable using the scrollwheel. Well, at least I think it’s a minor issue since only around 5% of our visitors are using IE7…

    If I should find a better solution, I sure will post it here!

    Thanks again!

  7. Andrey says:

    Thanks a lot, nice solution!
    For me it works in IE7/IE8 too.
    IE7 has it’s own issue (it shows the scrollbars in the shadowbox but I believe it’s another issue)

Leave a Reply