The
Lightbox Effect is named after a plugin (Lightbox) created by
Lokesh Dhakar.
OK, but what does it do?
It's an overlay on the current page, dimming the entire page with displayed content in the middle, with a button to close it. That's it! Nothing fancy. It's a simple and elegant method of housing content in the
Lightbox without having to let it take up precious real estate on the parent page.
Other versions may have fancy borders, loading animations and countless nifty little features. We're going to do none of those today, and just focus on the basics.
So why write our own?
It's fun! Always a good reason to do anything.
Also, I want to show you that it's not all that complex at its core. That is, if you just want a
Lightbox Effect without any funny stuff, it's possible with just a few lines of code. No clunky JavaScript libraries. Just good old-fashioned JavaScript and HTML, with a bit of CSS3 thrown in for shits and giggles.
So let's start with this bit of code.
<!DOCTYPE html>
<html>
<head>
<title>Lightbox test</title>
<style type="text/css">
.content_wrapper
{
width:80%;
height:80%;
border:1px solid #777777;
overflow:auto;
margin-left:auto;
margin-right:auto;
margin-top:5%;
background-color:#FFFFFF;
}
.screen_wrapper
{
position:fixed;
z-index:900;
background: rgba(0,0,0,0.8);
left:0px;
top:0px;
width:100%;
height:100%;
}
.btn_close
{
width:95%;
margin-left:auto;
margin-right:auto;
text-align:right;
color:#999999;
cursor:pointer;
}
.content_box
{
width:95%;
margin-left:auto;
margin-right:auto;
}
</style>
</head>
<body>
<div id="wrapper" class="screen_wrapper">
<div id="lb_wrapper" class="content_wrapper">
<div class="btn_close">
x close
</div>
<div id="lb_content" class="content_box">
</div>
</div>
</div>
<div>
<div>Photo 1</div>
<div>Photo 2</div>
<div>Photo 3</div>
<div>Photo 4</div>
<div>Photo 5</div> </div>
</body>
</html>
Open it in your browser and you should see a translucent grey film over your page, with a white box in the middle. There's no content at the moment, and there's a "x close" button on the right. It doesn't work either. You need to write script for that!
But first things first - this is what your
Lightbox looks like when visible. We're going to turn it
invisible. Make the following change to the CSS.
.screen_wrapper
{
position:fixed;
z-index:900;
background: rgba(0,0,0,0.8);
left:0px;
top:0px;
width:100%;
height:100%;
display:none;
}
Now when you refresh your browser, your screen is back to its pristine clickable self!
Didn't we just do that two months ago?
Ah, you've been paying
attention. Yes, we did pretty much the same thing for
LKY's Memorial. So
if you went through that one, the first part of this web tutorial
should be easier than Britney Spears on crack.
The difference here is that we're going to populate different content here on the fly. And, of course, in here we define proper CSS classes.
The
screen_wrapper class is the translucent black overlay on the screen. Look up
LKY's Memorial for the explanation regarding this bit of code.
The
content_wrapper class sits in the middle of the overlay and has the following properties.
-
width:80%,
height:80% fills up the screen with a little border around the edges, to show part of the screen beneath this layer.
-
border:1px solid #777777; gives the
content_wrapper a nice
grey border.
-
overflow:auto ensures that any content that goes outside this box will be available through the use of (very ugly but functional) scrollbars.
-
margin-left:auto,
margin-right:auto puts the box dab center of the overlay.
The
btn_close class is for the clickable element that closes your
Lightbox.
-
width:100% makes sure the div occupies the entire width of
content_wrapper.
-
text-align:right because you want the text to be aligned right, duh.
-
cursor:pointer to change the mouse cursor to a pointer when you roll over the area, indicating that this guy can be clicked on.
And the
content_box class is for the div we'll be populating with content!
-
width:100% makes sure the div occupies the entire width of
content_wrapper.
-
padding:20px makes sure your content doesn't stray too close to the borders, which would make for some really ugly effects.
Now for some data!
We need some JavaScript for this. Add this code to your
head tag.
<head>
<title>Lightbox test</title>
<style type="text/css">
.content_wrapper
{
width:80%;
height:80%;
border:1px solid #777777;
overflow:auto;
margin-left:auto;
margin-right:auto;
margin-top:5%;
background-color:#FFFFFF;
}
.screen_wrapper
{
position:fixed;
z-index:900;
background: rgba(0,0,0,0.8);
left:0px;
top:0px;
width:100%;
height:100%;
display:none;
}
.btn_close
{
width:100%;
text-align:right;
color:#999999;
cursor:pointer;
}
.content_box
{
width:100%;
padding:20px;
}
</style>
<script>
function open_lb(varid)
{
document.getElementById("wrapper").style.display="block";
document.getElementById("lb_content").innerHTML=content[varid];
}
function close_lbt()
{
document.getElementById("wrapper").style.display="none";
}
</script>
</head>
The
close_lb() function is straightforward - just set the
wrapper div's
display property to
none, thus rendering it - and all content within it - to vanish. For that to happen, you need to add this code in the HTML, like so.
<div class="btn_close" onclick="close_lb();">
x close
</div>
The
open_lb() function does a wee bit more - it sets the
wrapper div's
display property to
block, thus rendering it - and all content within it - to appear. And then it uses the
varid variable which was passed in as an argument, to populate the
lb_content div.
So let's create the content array, and fill it with data like so. The images are, of course, to be saved to the same folder as your HTML file.
<script>
var content=[];
content[0]="<img src='00.jpg' height='400' align='left'> <p style='text-align:justify'>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas ultricies viverra eros, et rhoncus nulla accumsan vitae. Praesent mauris lectus, mollis non feugiat ut, suscipit id arcu. Aliquam ac gravida ipsum, et pretium risus. Phasellus euismod venenatis pharetra. Sed at neque vitae ipsum facilisis elementum. Cras auctor nunc nisi, nec congue odio luctus vitae. Ut a nisl quis purus gravida faucibus. Etiam ut nunc sed dui convallis efficitur sit amet nec tortor. Pellentesque elit sem, aliquet id dui id, ultricies dictum eros. Suspendisse egestas, ante sit amet volutpat vestibulum, risus ex accumsan diam, quis varius leo diam et neque. Etiam in efficitur diam. </p><p style='text-align:justify'>Etiam eget massa lorem. Nulla congue maximus tincidunt. Vivamus at efficitur libero. Etiam a aliquet metus, sit amet fermentum neque. In id auctor erat. Praesent a lacus nibh. Donec gravida augue eu nisi varius, in lacinia justo feugiat. </p><p style='text-align:justify'>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus fringilla augue non mollis sagittis. Vestibulum ullamcorper odio sit amet fermentum cursus. Ut vestibulum libero at risus rutrum, in dictum nibh lobortis. Aliquam urna velit, posuere imperdiet hendrerit eget, accumsan in nulla. Aliquam quis fringilla ex. Nunc auctor sapien nisi, nec efficitur lorem sollicitudin vel. Quisque diam lacus, rutrum eu nunc in, sollicitudin auctor lacus.</p>";
content[1]="<img src='01.jpg' height='400' align='left'><p style='text-align:justify'>Aliquam erat volutpat. Maecenas id tristique libero. Aenean iaculis mi nec nisl tempor, sit amet lobortis nisi condimentum. Nulla ut odio quis velit pulvinar fringilla sit amet et nisi. Pellentesque sit amet faucibus massa, vel sodales mauris. Duis fringilla ut lacus sed ultricies. Suspendisse id auctor ipsum. Vestibulum lacinia nisl urna, quis mattis nulla placerat at. Curabitur sagittis risus nec euismod maximus. Morbi facilisis bibendum purus ac mattis. Vestibulum tempus efficitur massa, a convallis mi dapibus vitae. In pharetra vitae nulla eu malesuada. Ut aliquet volutpat tristique. Fusce pellentesque porta finibus. Aliquam erat volutpat. Maecenas id tristique libero. Aenean iaculis mi nec nisl tempor, sit amet lobortis nisi condimentum. Nulla ut odio quis velit pulvinar fringilla sit amet et nisi. Pellentesque sit amet faucibus massa, vel sodales mauris. Duis fringilla ut lacus sed ultricies. Suspendisse id auctor ipsum. Vestibulum lacinia nisl urna, quis mattis nulla placerat at. Curabitur sagittis risus nec euismod maximus. Morbi facilisis bibendum purus ac mattis. Vestibulum tempus efficitur massa, a convallis mi dapibus vitae. In pharetra vitae nulla eu malesuada. Ut aliquet volutpat tristique. Fusce pellentesque porta finibus. </p><p style='text-align:justify'>Sed ultricies tincidunt turpis, in feugiat mauris malesuada id. Morbi sit amet semper eros. Donec vestibulum sem quis congue luctus. Suspendisse turpis nulla, ornare nec erat et, tincidunt aliquet odio. Aliquam cursus felis nec sem volutpat fermentum. Aenean lacinia sapien sapien, sit amet tincidunt ligula eleifend at. Integer sit amet mi ac purus gravida tempus quis ac ipsum. Nullam ultrices eleifend enim, at rutrum lacus mollis vel. Donec sapien urna, pretium a augue ut, gravida tempus mi. Sed sit amet ullamcorper ante. Aliquam sagittis, justo in dictum auctor, nunc erat pellentesque magna, id facilisis tortor nisi eget ligula. Donec suscipit mattis velit. </p>";
content[2]="<img src='02.jpg' height='400' align='right'><p style='text-align:justify'>Maecenas eu risus aliquet libero maximus facilisis ornare ac sapien. Nam ac sapien eu nulla laoreet mollis ac id purus. Vivamus dapibus ex sit amet turpis faucibus, quis euismod mi semper. Fusce quis tristique turpis, in porttitor eros. Vivamus non velit laoreet, semper enim a, volutpat velit. In hac habitasse platea dictumst. In mollis purus ipsum, at ultrices eros aliquam nec. In vestibulum lorem id felis tempor semper. </p><p style='text-align:justify'>Aenean id leo ultrices, dignissim orci non, rhoncus augue. Etiam posuere bibendum magna, quis congue nibh dapibus eget. Praesent pharetra a nisl eu euismod. Pellentesque consequat fermentum magna ut pharetra. Proin nec scelerisque est, eget mattis tellus. Nullam rhoncus pretium dolor, in fringilla eros interdum eget. Nulla facilisi. </p><p style='text-align:justify'>Vestibulum tellus odio, efficitur quis convallis et, imperdiet sed odio. Etiam nec dui at justo gravida semper nec a ex. Nam tellus nisi, aliquet in ipsum hendrerit, vulputate finibus ligula. Aliquam vel quam est. Sed volutpat lectus tortor, a rutrum lorem cursus non. Vivamus vitae vestibulum nibh, vel maximus tortor. Aliquam nibh nulla, ornare eget posuere id, luctus non lacus. Sed felis nisl, pretium vel nisl sed, scelerisque iaculis tortor. Mauris ex mauris, sagittis at risus at, molestie feugiat metus. </p><p style='text-align:justify'>Praesent ac magna in quam accumsan interdum. Vivamus faucibus volutpat neque, id pellentesque massa finibus in. Aliquam dapibus sem vel semper mollis. Praesent nec turpis purus. Mauris a diam congue, maximus nibh ac, ultrices ante. Ut a turpis mollis, placerat lacus eget, auctor eros. Nulla justo metus, ullamcorper eu tincidunt ut, lacinia a eros. Sed quis lobortis tortor. Ut pharetra ornare libero sit amet feugiat. Curabitur mattis augue quis tellus pulvinar scelerisque. Quisque eros risus, consectetur in pulvinar at, sollicitudin id sem. Quisque accumsan varius tortor, a semper ex pharetra ac. Mauris tristique vitae ante nec sagittis. Morbi facilisis felis non vulputate porta. Mauris dignissim, tellus sed dictum laoreet, erat leo eleifend leo, ut mollis dui elit non diam. </p>";
content[3]="<img src='03.jpg' height='400' align='right'><p style='text-align:justify'>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ornare libero eu varius commodo. Nulla et bibendum lectus, eget rutrum ante. Integer lobortis pulvinar quam eget malesuada. Cras interdum nulla at elit pharetra ultrices. Integer pellentesque, sem id tempus viverra, urna dui semper sapien, et facilisis sapien diam ac sem. Phasellus sagittis est vel euismod laoreet. Nullam semper non leo ut facilisis. Suspendisse eu ante turpis. Donec vel lorem nisl. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Etiam lacinia ac massa eu sodales. Aliquam tempor nisl erat, a ullamcorper purus venenatis sit amet. Duis lobortis, dolor et elementum faucibus, metus enim faucibus mi, eu posuere massa augue sit amet nulla. In aliquam consectetur ex. Praesent id nisl odio. </p><p style='text-align:justify'>Suspendisse euismod lacinia ligula. Etiam bibendum condimentum odio eget vulputate. Cras ultrices odio vel lacus rutrum, et tempor arcu porttitor. Vestibulum suscipit ante mattis sapien pretium, ac facilisis quam iaculis. Proin sapien ligula, condimentum eget dui posuere, vestibulum interdum purus. Morbi euismod, ipsum ut rhoncus congue, lectus turpis interdum justo, sit amet dapibus ex neque nec lectus. Sed vel rhoncus orci, sed porttitor ipsum. Nunc arcu ligula, volutpat eu ante euismod, maximus convallis sapien. Maecenas dignissim vel massa in fermentum. Pellentesque varius sapien ac maximus laoreet. Fusce eu eros porta, rhoncus massa fringilla, cursus leo. Etiam tempus libero non augue laoreet ultrices. </p>";
content[4]="<img src='04.jpg' height='400' align='left'><p style='text-align:justify'>Quisque vehicula eros sed sem imperdiet, id dignissim erat dapibus. Pellentesque nec varius justo. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Curabitur fringilla sodales diam a dictum. Curabitur non lacus efficitur, rutrum nisi vitae, accumsan urna. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cras gravida mauris libero, a pretium mi rutrum eget. Aliquam velit eros, vulputate ut ultrices eu, auctor tincidunt massa. Praesent posuere cursus vulputate. Maecenas suscipit lorem nec justo iaculis congue. In volutpat sodales interdum. Aenean vel urna non ante dapibus elementum eget ut neque. </p><p style='text-align:justify'>Vestibulum at lectus id tortor sagittis blandit. Vivamus in sapien sit amet justo tincidunt finibus. Pellentesque iaculis, sapien eget consectetur consectetur, sapien leo pretium libero, nec ultricies libero purus quis quam. Duis pharetra sem eget leo cursus maximus. In suscipit, nisl nec tempus volutpat, eros justo cursus leo, malesuada ullamcorper neque ipsum vel augue. Morbi cursus dui vitae nulla viverra vulputate. Donec in hendrerit lorem. </p><p style='text-align:justify'>Morbi non neque vel lacus vehicula vulputate. Phasellus pulvinar, nibh at fringilla condimentum, nulla ipsum consequat arcu, et sollicitudin nibh ipsum quis justo. Quisque imperdiet ligula et est sagittis, vel ultrices eros mattis. Fusce orci elit, fermentum a accumsan eget, porta auctor augue. Nam risus dui, sodales at tristique aliquam, ultrices non elit. Mauris vitae pharetra dui. Cras sollicitudin dignissim justo, non blandit eros pharetra sed. Etiam tristique metus magna, et blandit lacus lacinia et. Integer ac mauris et est pulvinar ultricies sit amet ut purus. Aliquam varius velit sollicitudin, ornare dolor quis, scelerisque tortor. Curabitur sed dolor dapibus, semper risus non, convallis nibh. Morbi suscipit sapien est, quis pellentesque nibh blandit sed. Quisque efficitur fermentum mauris, eu ultrices dolor dictum eget. Vivamus et odio enim. Aliquam pharetra vehicula vestibulum. </p>";
function open_lb(varid)
{
document.getElementById("wrapper").style.display="block";
document.getElementById("lb_content").innerHTML=content[varid];
}
function close_lb()
{
document.getElementById("wrapper").style.display="none";
}
</script>
Yep, I'm going to use this opportunity to plug pictures of my nephew Paul. Do I hear a collective groan from the audience? Well, fuck you. If you guys can post your bimbotic selfies and pics of your cats and your lunch all over Facebook and Instagram, I am damn well going to post pics of my baby nephew. Deal with it!
For the purposes of brevity, the actual HTML code for the
content array is listed here.
Now adjust your HTML so that clicking on the labels "Photo 1" to "Photo 5" brings up the
Lightbox.
<div>
<div><a href="#" onclick="open_lb(0);">Photo 1</a></div>
<div><a href="#" onclick="open_lb(1);">Photo 2</a></div>
<div><a href="#" onclick="open_lb(2);">Photo 3</a></div>
<div><a href="#" onclick="open_lb(3);">Photo 4</a></div>
<div><a href="#" onclick="open_lb(4);">Photo 5</a></div>
</div>
Now refresh your browser. Click on the links. Do the individual photos pop up in the
Lightbox? And can you close the
Lightbox by clicking on "x close"? Well, congratulations then! You've just made your first
Lightbox!
Next
It's a boring
Lightbox, truth be told. Let's jazz it up a little with some good old CSS3!