I'm a beginner and I'm trying to make a lightbox web gallery with jquery. It's a basic gallery, when you click on an image (thumbnail) in the list, jquery shows the hidden lightbox div, which makes the background dark and brings up the current image (full size) with a white border, with the next/prev and close buttons on the border. My next/prev buttons work only once, and I don't know how to fix it. (I haven't done anything with the prev button, so that obviously doesn't work at all.) Any ideas? Here's the code:
html
<ul class="gallery">
<li> <a href="img/gallery/fullsize/full_foto001.jpg" class="lightbox_trigger"> <img src="img/gallery/thumbnail/thumbnail_foto001.jpg"> </a> </li>
<li> <a href="img/gallery/fullsize/full_foto002.jpg" class="lightbox_trigger"> <img src="img/gallery/thumbnail/thumbnail_foto002.jpg"> </a> </li>
<li> <a href="img/gallery/fullsize/full_foto003.jpg" class="lightbox_trigger"> <img src="img/gallery/thumbnail/thumbnail_foto003.jpg"> </a> </li>
<li> <a href="img/gallery/fullsize/full_foto004.jpg" class="lightbox_trigger"> <img src="img/gallery/thumbnail/thumbnail_foto004.jpg"> </a> </li>
<li> <a href="img/gallery/fullsize/full_foto005.jpg" class="lightbox_trigger"> <img src="img/gallery/thumbnail/thumbnail_foto005.jpg"> </a> </li>
<li> <a href="img/gallery/fullsize/full_foto006.jpg" class="lightbox_trigger"> <img src="img/gallery/thumbnail/thumbnail_foto006.jpg"> </a> </li>
<li> <a href="img/gallery/fullsize/full_foto007.jpg" class="lightbox_trigger"> <img src="img/gallery/thumbnail/thumbnail_foto007.jpg"> </a> </li>
<li> <a href="img/gallery/fullsize/full_foto008.jpg" class="lightbox_trigger"> <img src="img/gallery/thumbnail/thumbnail_foto008.jpg"> </a> </li>
<li> <a href="img/gallery/fullsize/full_foto009.jpg" class="lightbox_trigger"> <img src="img/gallery/thumbnail/thumbnail_foto009.jpg"> </a> </li>
</ul>
<div id="lightbox">
<div id="gallery_content">
</div>
jquery
$('.lightbox_trigger').click(function(e) {
//prevent default action (hyperlink)
e.preventDefault();
$('#lightbox').show('slow');
var image_href = $(this).attr("href");
var image_href2 = $(this).parent().next('li').children('a').attr("href");
$('#gallery_content').html('<img src="' + image_href + '" />' +
'<div id="gallery_close"> <img src="img/gallery/lightbox/close.png"/> </div>' +
'<div id="gallery_next"> <img src="img/gallery/lightbox/next.png"/> </div>'+
'<div id="gallery_prev"> <img src="img/gallery/lightbox/previous.png"/> </div>');
//next
$(document).on('click', '#gallery_next', function(){
$('#gallery_content').html('<img src="' + image_href2 + '" />' +
'<div id="gallery_close"> <img src="img/gallery/lightbox/close.png"/> </div>' +
'<div id="gallery_next"> <img src="img/gallery/lightbox/next.png"/> </div>'+
'<div id="gallery_prev"> <img src="img/gallery/lightbox/previous.png"/> </div>');
});
//previous
$('#gallery_prev').click(function(){
});
});
I figured it out. I don't know how "elegant" it is, but it works. Here it is:
$(document).ready(function() {
$('.lightbox_trigger').click(function(e) {
//prevent default action (hyperlink)
e.preventDefault();
$('#lightbox').show('slow');
$(this).attr('class','this');
var image_href = $(this).attr("href");
var image_href2 = $('.this').parent().next('li').children('a').attr("href");
$('#gallery_content').html('<img src="' + image_href + '" />' +
'<div id="gallery_close"> <img src="img/gallery/lightbox/close.png"/> </div>' +
'<div id="gallery_next"> <img src="img/gallery/lightbox/next.png"/> </div>'+
'<div id="gallery_prev"> <img src="img/gallery/lightbox/previous.png"/> </div>');
});
//next
$(document).on('click', '#gallery_next', function(){
var image_href2 = $('.this').parent().next('li').children('a').attr("href");
$('#gallery_content').html('<img src="' + image_href2 + '" />' +
'<div id="gallery_close"> <img src="img/gallery/lightbox/close.png"/> </div>' +
'<div id="gallery_next"> <img src="img/gallery/lightbox/next.png"/> </div>'+
'<div id="gallery_prev"> <img src="img/gallery/lightbox/previous.png"/> </div>');
$('.this').attr('class', 'remove');
$('.this').removeAttr('class', 'this');
$('.remove').parent().next('li').children('a').attr('class', 'this');
$('.remove').removeAttr('class', 'remove');
});
//previous
//hide
$(document).on("click", "#gallery_close", function(event){
$('.lightbox_trigger').removeAttr('class', 'this');
$('.lightbox_trigger').removeAttr('class', 'remove');
$('#lightbox').hide();
});
});