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!

http://www.iandevlin.com/blog/wp-content/plugins/sociofluid/images/digg_24.png http://www.iandevlin.com/blog/wp-content/plugins/sociofluid/images/reddit_24.png http://www.iandevlin.com/blog/wp-content/plugins/sociofluid/images/stumbleupon_24.png http://www.iandevlin.com/blog/wp-content/plugins/sociofluid/images/delicious_24.png http://www.iandevlin.com/blog/wp-content/plugins/sociofluid/images/technorati_24.png http://www.iandevlin.com/blog/wp-content/plugins/sociofluid/images/google_24.png http://www.iandevlin.com/blog/wp-content/plugins/sociofluid/images/facebook_24.png http://www.iandevlin.com/blog/wp-content/plugins/sociofluid/images/twitter_24.png

Related posts:

  1. Shadowbox and Internet Explorer 8
  2. Dynamically changing the background image applied with the DD_belatedPNG IE6 script

2 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.

Leave a Reply