Tiffany R. White Blog

Your friend Tiffany's tiny little weblog.

Day 4: Lightbox

2015-08-11 Tiffany WhiteDaily Progress


So yesterday we made a simple lightbox for a set of images, which I found hard to follow the lesson somehow.

Assigning jQuery elements to a variable has proven to be extremely useful. For instance this bit of code:

var $overlay = $('<div id="overlay"></div></div>');
var $image = $("<img>");
var $caption = $("");

// Add overly

// An image
 // A caption


We don’t have to continually write out jQuery objects, we can just refer to them in the variables that preceded the block of code where we need them.

We did more appending and we learned about the


method which we passed in the string, (“href”) after attaching it to


. We wanted to make sure the image we clicked on in the image gallery would execute the behavior we wanted, which was popping up the lightbox. Using


assured us that we were clicking the linked image attribute in the HTML, and it wouldn’t matter which image was clicked. Here is the last part of the code:

//Capture the click event on a link to an image
$("#imageGallery a").click(function(event){
   var imageLocation = $(this).attr("href");
 //Update overlay with the image linked in the link
   $image.attr("src", imageLocation);

//Show the overlay.

//Get child's alt attribute and set caption
   var captionText = $(this).children("img").attr("alt");


//When overlay is clicked
 //Hide the overlay