2 Lightbox JS: Fullsize Image Overlays
3 by Lokesh Dhakar - http://www.huddletogether.com
5 For more information on this script, visit:
6 http://huddletogether.com/projects/lightbox/
8 Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
9 (basically, do anything you want, just leave my name and link)
27 - addLoadEvent(initLightbox)
37 // If you would like to use a custom loading image or close button reference them in the next two lines.
38 var loadingImage = 'loading.gif';
39 var closeButton = 'close.gif';
47 // Returns array with x,y page scroll values.
48 // Core code from - quirksmode.org
50 function getPageScroll(){
54 if (self.pageYOffset) {
55 yScroll = self.pageYOffset;
56 } else if (document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict
57 yScroll = document.documentElement.scrollTop;
58 } else if (document.body) {// all other Explorers
59 yScroll = document.body.scrollTop;
62 arrayPageScroll = new Array('',yScroll)
63 return arrayPageScroll;
70 // Returns array with page width, height and window width, height
71 // Core code from - quirksmode.org
72 // Edit for Firefox by pHaez
74 function getPageSize(){
78 if (window.innerHeight && window.scrollMaxY) {
79 xScroll = document.body.scrollWidth;
80 yScroll = window.innerHeight + window.scrollMaxY;
81 } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
82 xScroll = document.body.scrollWidth;
83 yScroll = document.body.scrollHeight;
84 } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
85 xScroll = document.body.offsetWidth;
86 yScroll = document.body.offsetHeight;
89 var windowWidth, windowHeight;
90 if (self.innerHeight) { // all except Explorer
91 windowWidth = self.innerWidth;
92 windowHeight = self.innerHeight;
93 } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
94 windowWidth = document.documentElement.clientWidth;
95 windowHeight = document.documentElement.clientHeight;
96 } else if (document.body) { // other Explorers
97 windowWidth = document.body.clientWidth;
98 windowHeight = document.body.clientHeight;
101 // for small pages with total height less then height of the viewport
102 if(yScroll < windowHeight){
103 pageHeight = windowHeight;
105 pageHeight = yScroll;
108 // for small pages with total width less then width of the viewport
109 if(xScroll < windowWidth){
110 pageWidth = windowWidth;
116 arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
117 return arrayPageSize;
122 // pause(numberMillis)
123 // Pauses code execution for specified time. Uses busy code, not good.
124 // Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
126 function pause(numberMillis) {
127 var now = new Date();
128 var exitTime = now.getTime() + numberMillis;
131 if (now.getTime() > exitTime)
138 // Gets keycode. If 'x' is pressed then it hides the lightbox.
142 if (e == null) { // ie
143 keycode = event.keyCode;
147 key = String.fromCharCode(keycode).toLowerCase();
149 if(key == 'x'){ hideLightbox(); }
156 function listenKey () { document.onkeypress = getKey; }
161 // Preloads images. Pleaces new image in lightbox then centers and displays.
163 function showLightbox(objLink)
166 var objOverlay = document.getElementById('overlay');
167 var objLightbox = document.getElementById('lightbox');
168 var objCaption = document.getElementById('lightboxCaption');
169 var objImage = document.getElementById('lightboxImage');
170 var objLoadingImage = document.getElementById('loadingImage');
171 var objLightboxDetails = document.getElementById('lightboxDetails');
174 var arrayPageSize = getPageSize();
175 var arrayPageScroll = getPageScroll();
177 // center loadingImage if it exists
178 if (objLoadingImage) {
179 objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
180 objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
181 objLoadingImage.style.display = 'block';
184 // set height of Overlay to take up whole page and show
185 objOverlay.style.height = (arrayPageSize[1] + 'px');
186 objOverlay.style.display = 'block';
189 imgPreload = new Image();
191 imgPreload.onload=function(){
192 objImage.src = objLink.href;
194 // center lightbox and make sure that the top and left values are not negative
195 // and the image placed outside the viewport
196 var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
197 var lightboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);
199 objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
200 objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
203 objLightboxDetails.style.width = imgPreload.width + 'px';
205 if(objLink.getAttribute('title')){
206 objCaption.style.display = 'block';
207 //objCaption.style.width = imgPreload.width + 'px';
208 objCaption.innerHTML = objLink.getAttribute('title');
210 objCaption.style.display = 'none';
213 // A small pause between the image loading and displaying is required with IE,
214 // this prevents the previous image displaying for a short burst causing flicker.
215 if (navigator.appVersion.indexOf("MSIE")!=-1){
219 if (objLoadingImage) { objLoadingImage.style.display = 'none'; }
221 // Hide select boxes as they will 'peek' through the image in IE
222 selects = document.getElementsByTagName("select");
223 for (i = 0; i != selects.length; i++) {
224 selects[i].style.visibility = "hidden";
228 objLightbox.style.display = 'block';
230 // After image is loaded, update the overlay height as the new image might have
231 // increased the overall page height.
232 arrayPageSize = getPageSize();
233 objOverlay.style.height = (arrayPageSize[1] + 'px');
235 // Check for 'x' keypress
241 imgPreload.src = objLink.href;
252 function hideLightbox()
255 objOverlay = document.getElementById('overlay');
256 objLightbox = document.getElementById('lightbox');
258 // hide lightbox and overlay
259 objOverlay.style.display = 'none';
260 objLightbox.style.display = 'none';
262 // make select boxes visible
263 selects = document.getElementsByTagName("select");
264 for (i = 0; i != selects.length; i++) {
265 selects[i].style.visibility = "visible";
268 // disable keypress listener
269 document.onkeypress = '';
277 // Function runs on window load, going through link tags looking for rel="lightbox".
278 // These links receive onclick events that enable the lightbox display for their targets.
279 // The function also inserts html markup at the top of the page which will be used as a
280 // container for the overlay pattern and the inline image.
282 function initLightbox()
285 if (!document.getElementsByTagName){ return; }
286 var anchors = document.getElementsByTagName("a");
288 // loop through all anchor tags
289 for (var i=0; i<anchors.length; i++){
290 var anchor = anchors[i];
292 if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox")){
293 anchor.onclick = function () {showLightbox(this); return false;}
297 // the rest of this code inserts html at the top of the page that looks like this:
299 // <div id="overlay">
300 // <a href="#" onclick="hideLightbox(); return false;"><img id="loadingImage" /></a>
302 // <div id="lightbox">
303 // <a href="#" onclick="hideLightbox(); return false;" title="Click anywhere to close image">
304 // <img id="closeButton" />
305 // <img id="lightboxImage" />
307 // <div id="lightboxDetails">
308 // <div id="lightboxCaption"></div>
309 // <div id="keyboardMsg"></div>
313 var objBody = document.getElementsByTagName("body").item(0);
315 // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
316 var objOverlay = document.createElement("div");
317 objOverlay.setAttribute('id','overlay');
318 objOverlay.onclick = function () {hideLightbox(); return false;}
319 objOverlay.style.display = 'none';
320 objOverlay.style.position = 'absolute';
321 objOverlay.style.top = '0';
322 objOverlay.style.left = '0';
323 objOverlay.style.zIndex = '90';
324 objOverlay.style.width = '100%';
325 objBody.insertBefore(objOverlay, objBody.firstChild);
327 var arrayPageSize = getPageSize();
328 var arrayPageScroll = getPageScroll();
330 // preload and create loader image
331 var imgPreloader = new Image();
333 // if loader image found, create link to hide lightbox and create loadingimage
334 imgPreloader.onload=function(){
336 var objLoadingImageLink = document.createElement("a");
337 objLoadingImageLink.setAttribute('href','#');
338 objLoadingImageLink.onclick = function () {hideLightbox(); return false;}
339 objOverlay.appendChild(objLoadingImageLink);
341 var objLoadingImage = document.createElement("img");
342 objLoadingImage.src = incPrefix + loadingImage;
343 objLoadingImage.setAttribute('id','loadingImage');
344 objLoadingImage.style.position = 'absolute';
345 objLoadingImage.style.zIndex = '150';
346 objLoadingImageLink.appendChild(objLoadingImage);
348 imgPreloader.onload=function(){}; // clear onLoad, as IE will flip out w/animated gifs
353 imgPreloader.src = incPrefix + loadingImage;
355 // create lightbox div, same note about styles as above
356 var objLightbox = document.createElement("div");
357 objLightbox.setAttribute('id','lightbox');
358 objLightbox.style.display = 'none';
359 objLightbox.style.position = 'absolute';
360 objLightbox.style.zIndex = '100';
361 objBody.insertBefore(objLightbox, objOverlay.nextSibling);
364 var objLink = document.createElement("a");
365 objLink.setAttribute('href','#');
366 objLink.setAttribute('title','Click to close');
367 objLink.onclick = function () {hideLightbox(); return false;}
368 objLightbox.appendChild(objLink);
370 // preload and create close button image
371 var imgPreloadCloseButton = new Image();
373 // if close button image found,
374 imgPreloadCloseButton.onload=function(){
376 var objCloseButton = document.createElement("img");
377 objCloseButton.src = incPrefix + closeButton;
378 objCloseButton.setAttribute('id','closeButton');
379 objCloseButton.style.position = 'absolute';
380 objCloseButton.style.zIndex = '200';
381 objLink.appendChild(objCloseButton);
386 imgPreloadCloseButton.src = incPrefix + closeButton;
389 var objImage = document.createElement("img");
390 objImage.setAttribute('id','lightboxImage');
391 objLink.appendChild(objImage);
393 // create details div, a container for the caption and keyboard message
394 var objLightboxDetails = document.createElement("div");
395 objLightboxDetails.setAttribute('id','lightboxDetails');
396 objLightbox.appendChild(objLightboxDetails);
399 var objCaption = document.createElement("div");
400 objCaption.setAttribute('id','lightboxCaption');
401 objCaption.style.display = 'none';
402 objLightboxDetails.appendChild(objCaption);
404 // create keyboard message
405 var objKeyboardMsg = document.createElement("div");
406 objKeyboardMsg.setAttribute('id','keyboardMsg');
407 objKeyboardMsg.innerHTML = 'press <kbd>x</kbd> to close';
408 objLightboxDetails.appendChild(objKeyboardMsg);
418 // Adds event to window.onload without overwriting currently assigned onload functions.
419 // Function found at Simon Willison's weblog - http://simon.incutio.com/
421 function addLoadEvent(func)
423 var oldonload = window.onload;
424 if (typeof window.onload != 'function'){
425 window.onload = func;
427 window.onload = function(){
437 addLoadEvent(initLightbox); // run initLightbox onLoad