Advertisement
itskrystalized

popup lightbox 02# >> javascript

Jun 28th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. // Create a lightbox
  3.  
  4. var $lightbox = $("<div class='lightbox'></div>");
  5. var $img = $("<img>");
  6. var $caption = $("<p class='caption'></p>");
  7.  
  8.  
  9. // Add image and caption to lightbox
  10.  
  11. $lightbox
  12.     .append($img)
  13.     .append($caption);
  14.  
  15. // Add lighbox to document
  16.  
  17. $('body').append($lightbox);
  18.  
  19.  
  20. $('.gallery li').click(function (e) {
  21.     e.preventDefault();
  22.  
  23.     // Get image link and description
  24.     var src = $(this).children('img').attr("src");
  25.     var cap = $(this).children('img').attr("alt");
  26.  
  27.     // Add data to lighbox
  28.  
  29.     $img.attr('src',src);
  30.     $caption.text(cap);
  31.  
  32.     // $lightbox.append('<img src="' + src + '"></img><p class="caption">' + caption + '</p>');
  33.  
  34.     // Show lightbox
  35.  
  36.     $lightbox.show();
  37.  
  38.     $lightbox.click(function () {
  39.         $lightbox.hide();
  40.     });
  41. });
  42. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement