Skip to content

Instantly share code, notes, and snippets.

@ryankinal
Created July 11, 2012 16:35
Show Gist options
  • Save ryankinal/3091587 to your computer and use it in GitHub Desktop.
Save ryankinal/3091587 to your computer and use it in GitHub Desktop.
Really friggin' simple jQuery lightbox
$(function() {
var $blanket = $('#blanket'),
$close = $('#lightboxClose');
if ($blanket.size() === 0)
{
$blanket = $('<div />', {id: 'blanket'}).css({
'position': 'fixed',
'left': '0',
'top': '0',
'width': $(document).width() + 'px',
'height': $(document).height() + 'px',
'display': 'none'});
$('body').append($blanket);
}
if ($close.size() === 0)
{
$close = $('<div />', {id: 'lightboxClose', 'class': 'png'});
}
$(document).delegate('a[rel="lightbox"]', 'click', function() {
var href = this.href,
id = href.substring(href.lastIndexOf('#')),
$div = $(id);
if ($div.size() > 0)
{
$blanket.fadeTo(1000, 0.9);
$div.appendTo('body').fadeIn(600).css({
'position': 'fixed',
'left': '50%',
'top': '50%',
'margin-left': (($div.width() / 2) * -1) + 'px',
'margin-top': (($div.height() / 2) * -1) + 'px'
});
$close.appendTo($div).one('click', function() {
$blanket.fadeOut(600);
$div.fadeOut(600);
$blanket.unbind('click');
});
$blanket.one('click', function() {
$blanket.fadeOut(600);
$div.fadeOut(600);
$close.unbind('click');
});
}
return false;
});
});
<a href="#yourID" rel="lightbox">Link</a>
<div id="yourID">
<!--- content -->
</div>
@tchalvak
Copy link

+1

@ryankinal
Copy link
Author

This is actually pretty inefficient code. There's plenty that could be done outside of the click handler.

@tchalvak
Copy link

tchalvak commented Jul 31, 2012 via email

@ryankinal
Copy link
Author

Yeah, I've done a little with Mustache. The thing that probably intrigues me the most is the fact that I could use the same templating engine on the client side (JS) and on the server side (PHP). I've still mostly stuck with Smarty, but I'd like to give Mustache another look.

Also, I've added another, slightly more efficient version of this lightbox.

@tchalvak
Copy link

tchalvak commented Jul 31, 2012 via email

@tchalvak
Copy link

tchalvak commented Jul 31, 2012 via email

@ryankinal
Copy link
Author

http://www.mckissock.com/real-estate/florida/default.aspx

"Request a FREE Info Kit" or "Watch the Video"

@tchalvak
Copy link

tchalvak commented Jul 31, 2012 via email

@tchalvak
Copy link

tchalvak commented Sep 4, 2012

By the way, I'm trying out mustache templates in php, and while they really really simplify the templates, I wouldn't really recommend going in that direction on any substantial project where anyone other than yourself is going to work with the templates. Complicates things more than necessary, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment