Hamoe

Untitled

Nov 13th, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.19 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>Simple lightbox Window</title>
  5.         <style>
  6.         body {
  7.             margin:0;
  8.             padding:0;
  9.             background:#efefef;
  10.             text-align:center; /* center div in IE */
  11.             for ie div centering, reset in wrapper  */
  12.         }
  13.         #wrapper {
  14.             width:600px;
  15.             margin:0 auto;
  16.             border-radius:0 0 5px 5px;
  17.             -moz-border-radius:0 0 5px 5px;
  18.             -webkit-border-radius: 0 0 5px 5px;
  19.             background:#fff;
  20.             border:1px solid #ccc;
  21.             padding:25px;
  22.             border-top:none;
  23.             box-shadow:0 0 5px #ccc;
  24.             -moz-box-shadow:0 0 5px #ccc;
  25.             -webkit-box-shadow:0 0 5px #ccc;
  26.             text-align:left;
  27.         }
  28.         #lightbox {
  29.             position:fixed; /* keeps the lightbox window in the current viewport */
  30.             top:0;
  31.             left:0;
  32.             width:100%;
  33.             height:100%;
  34.             background:url(overlay.png) repeat;
  35.             text-align:center;
  36.         }
  37.         #lightbox p {
  38.             text-align:right;
  39.             color:#fff;
  40.             margin-right:20px;
  41.             font-size:12px;
  42.         }
  43.         #lightbox img {
  44.             box-shadow:0 0 25px #111;
  45.             -webkit-box-shadow:0 0 25px #111;
  46.             -moz-box-shadow:0 0 25px #111;
  47.             max-width:940px;
  48.         }
  49.         </style>
  50.     </head>
  51. <body>
  52.  
  53. <div id="wrapper">
  54.     <h1>Simple lightbox Window</h1>
  55.     <p>Here are the image links for testing:
  56.         <ul>
  57.             <li>
  58.                 <a href="http://farm7.static.flickr.com/6130/5935338876_47b61c93a5.jpg" class="lightbox_trigger">
  59.                 Picture 1
  60.                 </a>
  61.             </li>
  62.             <li>
  63.                 <a href="http://farm7.static.flickr.com/6020/5924329054_4bdc419c3a_o.jpg" class="lightbox_trigger">
  64.                 Picture 2
  65.                 </a>
  66.             </li>
  67.             <li>
  68.                 <a href="http://farm7.static.flickr.com/6020/5931933181_ddb737e528.jpg" class="lightbox_trigger">
  69.                 Picture 3
  70.                 </a>
  71.             </li>  
  72.         </ul>
  73.      </p>
  74. </div> <!-- #/wrapper -->
  75.  
  76. <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
  77. <script>
  78. jQuery(document).ready(function($) {
  79.    
  80.     $('.lightbox_trigger').click(function(e) {
  81.        
  82.         //prevent default action (hyperlink)
  83.         e.preventDefault();
  84.        
  85.         //Get clicked link href
  86.         var image_href = $(this).attr("href");
  87.        
  88.         /*  
  89.         If the lightbox window HTML already exists in document,
  90.         change the img src to to match the href of whatever link was clicked
  91.        
  92.         If the lightbox window HTML doesn't exists, create it and insert it.
  93.         (This will only happen the first time around)
  94.         */
  95.        
  96.         if ($('#lightbox').length > 0) { // #lightbox exists
  97.            
  98.             //place href as img src value
  99.             $('#content').html('<img src="' + image_href + '" />');
  100.            
  101.             //show lightbox window - you could use .show('fast') for a transition
  102.             $('#lightbox').show();
  103.         }
  104.        
  105.         else { //#lightbox does not exist - create and insert (runs 1st time only)
  106.            
  107.             //create HTML markup for lightbox window
  108.             var lightbox =
  109.             '<div id="lightbox">' +
  110.                 '<p>Click to close</p>' +
  111.                 '<div id="content">' + //insert clicked link's href into img src
  112.                     '<img src="' + image_href +'" />' +
  113.                 '</div>' + 
  114.             '</div>';
  115.                
  116.             //insert lightbox HTML into page
  117.             $('body').append(lightbox);
  118.         }
  119.        
  120.     });
  121.    
  122.     Click anywhere on the page to get rid of lightbox window
  123.     $('#lightbox').live('click', function() { //must use live, as the lightbox element is inserted into the DOM
  124.         $('#lightbox').hide();
  125.     });
  126.  
  127. });
  128. </script>
  129. </body>
  130. </html>
Advertisement
Add Comment
Please, Sign In to add comment