The Main Part Box & JavaScript
Well previously I posted on the basics of creating a lightbox, I shall explain the code for one of my most basic creations as once you understand this you can take it a lot further.
So I shall start with the JavaScript:
function lightbox(id1){
if(document.getElementById(id1).style.display==”block”){
document.getElementById(id1).style.display=”none”;
document.getElementById(‘lightbox’).style.display=”none”;
}else{
document.getElementById(id1).style.display=”block”;
document.getElementById(‘lightbox’).style.display=”block”;
}
}
Essentiall all it is doing is checking if id1 is already open if its shown as a CSS display block it will close id1 and lightbox id, else it will open them.
This keeps the script nice and short no bloated effects like Lightbox2 or jQuery lightbox, now in your html all you need to do is:
<div id=”mybox” class=”lightbox” style=”display:none;”>
stuff you want in lightbox
<a href=”javascript:lightbox(‘mybox’)”>Close Lightbox</a>
</div>
You can see I’ve set display:none; this is very important just because we are hiding the box, yeah not good for the search engines but for now it works, when you get more advanced I would have a class called hide, and apply it to the div tag just to keep google happy.
Now to open it:
<a href=”javascript:lightbox(‘mybox’)”>Open Lightbox</a>
So all we’re passing the function is the box ID (mybox) which will then open and we can use the EXACT same link to close it again!
![]()
Centering the Lightbox
Well we have the basics we now want it to have some nice effects.
.lightbox{
position:fixed;
height:200px;
width:500px;
top:50%;
left:50%;
margin-top:-100px;
margin-left:-250px;
}
So we’re setting the box as fixed so it scrolls with the page, and to get the full effect of centering it you need to set a height and width but when you enter the more advanced stages of lightbox you can just get the dimensions through JavaScript.
Now the essential part centering the box, top 50% and left 50% and then half the width and height which you can then offset in the margin, making it centered!
The Grey Background
Okay not the hardest part but it has the biggest effect, you can either have a white lightbox or black.
All you need is:
<div id=”lightbox” style=”position:fixed; top:0px; left:0px; width:100%; height:100%; background-image:(‘images/grey.png’);”> </div>
Your png file just needs to be 50% opacity #000000 and saved as a transparent png, this will then open when the JavaScript is triggered.
Not a huge fan of using pngs.
Check this one out.
http://www.emanueleferonato.com/downloads/lightbox.html
I do love CSS opacity, and is my 1st choice for a plain background.
But not many people seem to do this but it makes a amazing effect, using a actual background image with like stripes or some nice patten it can look really good!
long time no see sharky dude. Glad to see the blogs still up.
Backgruond image – not a good idea mate! Stick to opacity CSS filters
Ah but then it’s rather bland, I did a website recently with a PNG (thinking back to it would of been nicer as a GIF) but anyway it was a stripy over lay, and it really did look top bollocks!
You can choose any patterns you like with a image, but with the CSS opacity you’re really limited, plus you been at least what 4 different CSS styles now just to get a transparency cross browser as I understand IE8 now is different opacity call!
Your website is looking fantastic N2K!!
Massive improvement, but I do miss the old Ajax search you built that really did stand out!
Some really nice photos you have their as well.
thanks for this tips