<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Now we need our thumbnail image.
<img src="http://premium.imagesocket.com/images/2013/07/19/2637029-5czf.jpg" width="70" height="70">
We need to know this image should be targeted by our lightbox so we need to add a class of
lightbox
<img src="http://premium.imagesocket.com/images/2013/07/19/2637029-5czf.jpg" width="70" height="70" class="lightbox">
For all your images you just need to continue to add the
class="lightbox"
$(document).ready(function(){
$('.lightbox').click(function(e){
// This adds a few DIV's to the body to hold our lightboxed image
$('body').append('<div class="document_backdrop"></div><div class="lb_container"><img src=""></div>');
// This fades our backdrop so we get the nice dark look to the webpage
$('.document_backdrop').animate({'opacity':'.50'}, 300, 'linear');
// This sets the src of our lightboxed image
$(".lb_container img").attr("src",$(this).prop("src"));
// This makes our backdrop and lightboxed image show
$('.document_backdrop, .lb_container').css('display', 'block');
// Due to different layouts this ensures the backdrop fills the whole height of the webpage.
$('.document_backdrop').css('height',$(document).height());
// This centers the image both horizontally and vertically - see the following function.
$('.lb_container').center();
});
});
In the function above we made a call to
center(), the function below gets the width and height of the visible viewport in the browser and then places the image in the exact centre
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + $(window).scrollTop()) + "px");
this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
return this;
}
We also need to be able to let the visitor actually close the window, so we need this little function. No matter where they click the lightbox will be closed.
$(document).on('click','.lb_container,.document_backdrop',function(){
// A little animation to hide the lightbox
$('.document_backdrop, .lb_container').animate({'opacity':'0'}, 300, 'linear', function(){
$('.document_backdrop, .lb_container').css('display', 'none');
});
// Finally remove the lightbox from the page completely.
$('.document_backdrop,.lb_container').remove()
});
Finally we just need some CSS to make everything look hunky dory
.document_backdrop{
position:absolute;
top:0px;
left:0px;
width:100%;
background:#000;
opacity: .0;
filter:alpha(opacity=0);
z-index:999998;
}
.lb_container{
position:absolute;
background:#ffffff;
z-index:999999;
padding:10px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
.lb_container img{
max-width:300px;
max-height:200px;
}
$(document).ready(function(){
$('.lightbox').click(function(e){
// This adds a few DIV's to the body to hold our lightboxed image
$('body').append('<div class="document_backdrop"></div><div class="lb_container"><img src=""></div>');
// This fades our backdrop so we get the nice dark look to the webpage
$('.document_backdrop').animate({'opacity':'.50'}, 300, 'linear');
// This sets the src of our lightboxed image
$(".lb_container img").attr("src",$(this).prop("src"));
// This makes our backdrop and lightboxed image show
$('.document_backdrop, .lb_container').css('display', 'block');
// Due to different layouts this ensures the backdrop fills the whole height of the webpage.
$('.document_backdrop').css('height',$(document).height());
// This centers the image both horizontally and vertically - see the following function.
$('.lb_container').center();
});
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + $(window).scrollTop()) + "px");
this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
return this;
}
$(document).on('click','.lb_container,.document_backdrop',function(){
// A little animation to hide the lightbox
$('.document_backdrop, .lb_container').animate({'opacity':'0'}, 300, 'linear', function(){
$('.document_backdrop, .lb_container').css('display', 'none');
});
// Finally remove the lightbox from the page completely.
$('.document_backdrop,.lb_container').remove()
});
});
Add this css
.document_backdrop{
position:absolute;
top:0px;
left:0px;
width:100%;
background:#000;
opacity: .0;
filter:alpha(opacity=0);
z-index:999998;
}
.lb_container{
position:absolute;
background:#ffffff;
z-index:999999;
padding:10px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
.lb_container img{
max-width:300px;
max-height:200px;
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
Just a question, is it possible to to put a little x in the top right hand corner so that people know how to get out of the lightbox?
Author
Commented:Open in new window
Replace thisOpen in new window
withOpen in new window