jQuery Lightbox / Modal

There are SO MANY different types of lightbox scripts in the wild, and while most of them are absolutely fantastic (shadowbox being one of my favorites) most of them are very limited in the control that they give you over the style, function and code. To make things worse, most of these lightbox scripts are also 5k or bigger, which is 5k more than you need sometimes. If you’re like me, and you do a lot of customization or want to tweak your code to be as fast as possible, then you’ll want this simple little jquery lightbox plugin. Style your lightbox exactly how you’d like and then simple invoke the modal() function and you’ll be able to trigger the lightbox.

jQuery.fn.modal = function() {
	return this.each(function(){
	var ah = $(this).height();
        var wh = $(window).height();
        var nh = (wh/2) - (ah/2);

        var aw = $(this).width();
        var ww = $(window).width();
        var nw = (ww/2) - (aw/2);

        // Trim body height, append overlay, and disable scrolling (this can be undone with a custom close button)
        $(".overlay").fadeTo("slow", .8);
        $(this).css({'top':nh, 'left':nw}).fadeIn("slow");

This code is what i use for the overlay, you can customize as you’d like. The actual div is appended to the body with JavaScript.

.overlay { 
	display: block;
	position: absolute;
	top: 0%;
	left: 0%;
	width: 100%;
	height: 110%;
	background-color: black;
	-moz-opacity: 0.8;
	filter: alpha(opacity=80);

This is an example of a modal/lightbox i did using this plugin. Very simple CSS:

.modal { 
	background: #00121b; 
	min-height: 54.5em;
	width: 56.5em; 
	border: 1px solid #111e24; 
	box-shadow: 0 0 1em #000;
	-moz-box-shadow: 0 0 1em #000;
	-webkit-box-shadow: 0 0 1em #000;
	display: none;
	position: absolute; 
	z-index: 1002; 

Leave a Reply

Your email address will not be published. Required fields are marked *

To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image