Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript jQuery Basics (2014) Creating a Simple Lightbox Perfect

Juan Martin
Juan Martin
14,335 Points

Adding a bit more functionality to the Lightbox :)

Hey guys, I would like to share the code for some functionality that I've added to the Lightbox.

This is my Workspace's Snapshot

Enjoy!

i used your code but here is some problem my overlay is not coming over on whole site it is just on the top of page in header.

Now its working, thanx for sharing your content. :)

Juan Martin
Juan Martin
14,335 Points

You're welcome, I'm glad it worked for you :)

me to have your work. How long you are on treehouse .?

Juan Martin
Juan Martin
14,335 Points

Since February 2014, it's awesome how they teach here!

yup!!! its really, an you please help me, i'm looking for media queries i searched and found that tutorial is for pro version can you please help me by sending your worksheet here from where i can learn or by sending any link through from i can learn , it will be helpful .please if you have.

Juan Martin
Juan Martin
14,335 Points

I don't have that... Sorry :(

it's ok, thax for this Juan , nice to chat with you. have a nice day.

5 Answers

Konrad Pilch
Konrad Pilch
2,435 Points

Nice work! keep it up

Andrei Punei
Andrei Punei
14,051 Points

This is awesome!. Just the thing I was searching for!

Tnx!

hey Juan Martin... thanks for sharing bro! Just what I was searching for.

Heyy Juan Martin .. Quick question about the code that you provided. I'm trying to figure out how to implement a click function that hides the overly when the user clicks on the overlay. The problem is when I add that to my jquery, the user can't use the Next/Previous buttons since It's part of the overlay. Any ideas how to solve this?

Juan Martin
Juan Martin
14,335 Points

Hello friend, sorry for answering so late. In order to make it work the way you want, you would need to add a wrapper for the inner content (image and buttons).

Juan Martin .. Im confused on how to do this? How can i add a wrapper if its not coded on my html page. I appreciate any tips.

 var $overlay = $("<div id=\"overlay\"></div>");
var $image = $("<img>");
var $caption = $("<p></p>");
var $nextButton = $("<button>Next</button>");
var $prevButton = $("<button>Prev</button>");
var $closeButton = $("<button>Close</button>");

// Append empty image container to overlay
$overlay.append($image);
// Append empty caption container to overlay
$overlay.append($caption);
// Append prev and next button
$overlay.append($prevButton);
$overlay.append($nextButton);
// Append close button
$overlay.append($closeButton);
// Add overlay

$("body").append($overlay);


// Array of image Objects for the images inside the imageGallery
var imgJSON = [];

// Array of each href attribute of the links inside imageGallery
var imageSrcArray = $(".gallery-container a").map(function() {
  return $(this).attr("href");
}).get();

// Array of each title attribute of the images inside imageGallery
var imageCaptionArray = $(".gallery-container a img").map(function() {
  return $(this).attr("alt");
}).get();


// Lets populate imageJSON
for (var i = 0; i < imageSrcArray.length; i++) {
  imgJSON.push({
    src: imageSrcArray[i],
    title: imageCaptionArray[i],
    location: i
  });
}

// function that returns the current image Object based on the src we're looking
function findCurrentImage(arrayOfObjects, src) {
  for (var i = 0; i < arrayOfObjects.length; i++) {
    var result = arrayOfObjects[i].src.indexOf(src);
    if (result > -1) {
      return arrayOfObjects[i];
    }
  }
}

// function that animates the image on the transition
function animateImage() {
  $image.hide();
  $image.fadeIn("fast");
}

// click event on the image's link
$(".gallery-container a").click(function(event) {
  // we prevent the browser's default event when clicking the link
  event.preventDefault();
  // we get the href attribute from the link and the alt attribute from the image we clicked
  var imageSrc = $(this).attr("href");
  var imageCaption = $(this).children("img").attr("alt");
  $image.attr("src", imageSrc);
  $caption.text(imageCaption);

  // Showing the overlay
  $overlay.fadeIn();

});

// click event for the close button
$closeButton.click(function() {
    $overlay.fadeOut();

});

// click event for the next button
$nextButton.click(function() {
  // we get the current src attribute from the image
  var imageCurrentSrc = $(this).siblings("img").attr("src");
  // based on the current src attribute we look for the image at the array of objects in order to get to get all of its attributes and work more organized
  var currentImage = findCurrentImage(imgJSON, imageCurrentSrc);
  // if we are at the end, we'll move to the first image updating src attribute of the image and setting new text for the parragraph inside the overlay
  if (currentImage.location === imgJSON.length - 1) {
    $image.attr("src", imgJSON[0].src);
    $caption.text(imgJSON[0].title);
  } else {
    // otherwise we move to the next image updating src attribute of the image and setting new text for the parragraph inside the overlay
    $image.attr("src", imgJSON[currentImage.location + 1].src);
    $caption.text(imgJSON[currentImage.location + 1].title);
  }
  // we animate the image transition
  animateImage();
});

// click event for the prev button
$prevButton.click(function() {
  // we get the current src attribute from the image
  var imageCurrentSrc = $(this).siblings("img").attr("src");
  // based on the current src attribute we look for the image at the array of objects in order to get to get all of its attributes and work more organized
  var currentImage = findCurrentImage(imgJSON, imageCurrentSrc);
  // if we are at the first image, we'll move to the last updating src attribute of the image and setting new text for the parragraph inside the overlay
  if (currentImage.location === 0) {
    $image.attr("src", imgJSON[imgJSON.length - 1].src);
    $caption.text(imgJSON[[imgJSON.length - 1]].title);
  } else {
    // otherwise well move to the previous image updating src attribute of the image and setting new text for the parragraph inside the overlay
    $image.attr("src", imgJSON[currentImage.location - 1].src);
    $caption.text(imgJSON[currentImage.location - 1].title);
  }
  // we animate the image transition
  animateImage();
});

});
Juan Martin
Juan Martin
14,335 Points

Sure, I can help you out my friend. I will upload a new Workspace Snapshot so you can see what I meant :)

Juan Martin
Juan Martin
14,335 Points

Ok, here it is:

Simple LightBox v1.1

I've updated the style.css and app.js files, new code will have a "NEW" comment above (or next to) it.

Let me know if you have any questions :)