Custom LightBox JavaScript

Jul 21, 2008

A few people have asked me how to implement the div popup with a LightBox effect, like the one used when you click the Filter by APML link in the top right corner of this page. It’s actually very easy and it’s 100% JavaScript.

First, we need to add the semi-transparent div to lie on top of the entire page. This is done like so:

var width = document.documentElement.clientWidth + document.documentElement.scrollLeft;

var layer = document.createElement('div');
layer.style.zIndex = 2;
layer.id = 'layer';
layer.style.position = 'absolute';
layer.style.top = '0px';
layer.style.left = '0px';
layer.style.height = document.documentElement.scrollHeight + 'px';
layer.style.width = width + 'px';
layer.style.backgroundColor = 'black';
layer.style.opacity = '.6';
layer.style.filter += ("progid:DXImageTransform.Microsoft.Alpha(opacity=60)");
document.body.appendChild(layer);
 

Next we add a div in the middle of the page on top of the semi-transparent layer.

var div = document.createElement('div');
div.style.zIndex = 3;
div.id = 'box';
div.style.position = (navigator.userAgent.indexOf('MSIE 6') > -1) ? 'absolute' : 'fixed';
div.style.top = '200px';
div.style.left = (width / 2) - (400 / 2) + 'px'; 
div.style.height = '50px';
div.style.width = '400px';
div.style.backgroundColor = 'white';
div.style.border = '2px solid silver';
div.style.padding = '20px';
document.body.appendChild(div); 

And finally we put some text and a link that closes the popup when clicked:

var p = document.createElement('p');
p.innerHTML = 'Some text';
div.appendChild(p);

var a = document.createElement('a');
a.innerHTML = 'Close window';
a.href = 'javascript:void(0)';
a.onclick = function()
{
  document.body.removeChild(document.getElementById('layer'));
  document.body.removeChild(document.getElementById('box'));
};

 
div.appendChild(a);

I’ve tested this in Firefox 2+, Internet Explorer 6+, Safari 3 and Opera 9.23+ and it works in all of them. Here is an example HTML page that uses this method. It's very simple to copy and modify.

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (7) -

Josh Stodola
Josh Stodola United States
7/22/2008 2:37:12 AM #

Cool, but mine is in the center of the screen, and it stays there when you resize the window Tong

(Click "Admin" link on my blog)

Ridhi
Ridhi India
7/23/2008 11:07:44 AM #

How to integrate BE.NET with a website.

Chad
Chad United Kingdom
7/24/2008 8:51:10 AM #

That works great, but have you ever tried doing a overflow hidden on all the content behind the div overlay so there's no scrollbars?

IE6/7 has caused me loads of grief doing this, all other major browsers actually do what i tell them when i say overflow: hidden and set width and height of div.  I guess IE still wants to style the stuff that should be hidden.

krunal
krunal India
7/27/2008 9:48:09 AM #

it's working in my site.

gr8 ;)

krunal

vardis
vardis
12/2/2008 7:51:38 PM #

Is there any way of doing it without the transparetn div? Just for preference.

leon guerrero
leon guerrero United States
1/14/2009 5:45:41 AM #

wow! Thanks for the Javascript code.  Can't wait to test this out later at home.

Joshua
Joshua United States
3/14/2009 5:22:32 PM #

Mads,

Great post.  The only correction I would have to make is on the width assignment.

If the document.body is greater than the size of the window, then currently, when you scroll right, you'll see that the layer mask doesn't cover the entire body. The layer mask is only the width of the client window and therefore scrolls to the right, revealing everything in the body that is to the left of the window.

Add this:

    if (document.body.clientWidth > width)
        width = document.body.clientWidth;

This will solve the problem.  You can see its pretty self explanatory.

This conditional statement works in IE 7 and FireFox 3. I haven't tested it on other browsers, but I don't see why it shouldn't work with them either.

Otherwise, again, thanks for the post.

Pingbacks and trackbacks (2)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.