I have never used a lightbox before, but recently started thinking about using a similar feature on my website. I found a ton of jquery libraries and various add-ons, but I am a big fan of writing my own code. When I started looking under the hood, I was surprised to realize that it appears to simply be a hidden html element that is displayed when a Javascript event listener is triggered. Am I right about this? Is there more than meets the eye?
Just wondering how it works. Thank you in advance for your consideration.
UPDATE
Great answers! I went with Ana’s response because the other considerations regarding the design go without saying. As far as the mechanisms, it really does appear to be a wonderfully understated device. Thanks to all who read, commented, and replied…
No, there really is not much more to it. However, I don’t really see the point of having the HTML element there from the beginning unless you don’t want to use JavaScript for your lightbox, which I don’t think it is the case here judging from your tags and which I’m not a fan of using in real projects, even though I think it is really cool that it can be done.
If you’re using JavaScript anyway in order to display the lightbox (which means that if JavaScript is disabled, your lightbox won’t get displayed even if it is there, loaded right from the beginning… so why load it if you can’t display it?), then it’s probably better to just create the lightbox element (and everything in it, and then append it to the page) only when you first want the lightbox to open.
What I mean by that is that you attach a click handler to the links (actually, I would attach it to the container an then check what was clicked, if it is a link, see what link it is and go further) and check whether you have a lightbox element. If you don’t, then you create it on the spot. If you already do, then you simply display it with whatever you need in it for that particular link that was clicked.
A basic lightbox example for an image gallery.
The HTML structure of the gallery would be:
The CSS:
The JavaScript: