1/* 2 Lightbox JS: Fullsize Image Overlays 3 by Lokesh Dhakar - http://www.huddletogether.com 4 5 For more information on this script, visit: 6 http://huddletogether.com/projects/lightbox/ 7 8 Script featured on Dynamic Drive code library Jan 24th, 06': 9 http://www.dynamicdrive.com 10 11 Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/ 12 (basically, do anything you want, just leave my name and link) 13 14 Table of Contents 15 ----------------- 16 Configuration 17 18 Functions 19 - getPageScroll() 20 - getPageSize() 21 - pause() 22 - getKey() 23 - listenKey() 24 - showLightbox() 25 - hideLightbox() 26 - initLightbox() 27 - addLoadEvent() 28 29 Function Calls 30 - addLoadEvent(initLightbox) 31 32*/ 33 34 35 36// 37// Configuration 38// 39 40// If you would like to use a custom loading image or close button reference them in the next two lines. 41// The custom images are only loaded when also jQuery is loaded already 42 if (jQuery) { 43 var jsFileLocation = jQuery('script[src*=lightbox]').attr('src'); // the js file path 44 jsFileLocation = jsFileLocation.replace('lightbox.js', ''); // the js folder path 45 46 var loadingImage = jsFileLocation + 'loading.gif'; 47 var closeButton = jsFileLocation + 'close.gif'; 48 } 49 50// 51// getPageScroll() 52// Returns array with x,y page scroll values. 53// Core code from - quirksmode.org 54// 55function getPageScroll(){ 56 57 var yScroll; 58 59 if (self.pageYOffset) { 60 yScroll = self.pageYOffset; 61 } else if (document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict 62 yScroll = document.documentElement.scrollTop; 63 } else if (document.body) {// all other Explorers 64 yScroll = document.body.scrollTop; 65 } 66 67 arrayPageScroll = new Array('',yScroll) 68 return arrayPageScroll; 69} 70 71 72 73// 74// getPageSize() 75// Returns array with page width, height and window width, height 76// Core code from - quirksmode.org 77// Edit for Firefox by pHaez 78// 79function getPageSize(){ 80 81 var xScroll, yScroll; 82 83 if (window.innerHeight && window.scrollMaxY) { 84 xScroll = document.body.scrollWidth; 85 yScroll = window.innerHeight + window.scrollMaxY; 86 } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac 87 xScroll = document.body.scrollWidth; 88 yScroll = document.body.scrollHeight; 89 } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari 90 xScroll = document.body.offsetWidth; 91 yScroll = document.body.offsetHeight; 92 } 93 94 var windowWidth, windowHeight; 95 if (self.innerHeight) { // all except Explorer 96 windowWidth = self.innerWidth; 97 windowHeight = self.innerHeight; 98 } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode 99 windowWidth = document.documentElement.clientWidth; 100 windowHeight = document.documentElement.clientHeight; 101 } else if (document.body) { // other Explorers 102 windowWidth = document.body.clientWidth; 103 windowHeight = document.body.clientHeight; 104 } 105 106 // for small pages with total height less then height of the viewport 107 if(yScroll < windowHeight){ 108 pageHeight = windowHeight; 109 } else { 110 pageHeight = yScroll; 111 } 112 113 // for small pages with total width less then width of the viewport 114 if(xScroll < windowWidth){ 115 pageWidth = windowWidth; 116 } else { 117 pageWidth = xScroll; 118 } 119 120 121 arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 122 return arrayPageSize; 123} 124 125 126// 127// pause(numberMillis) 128// Pauses code execution for specified time. Uses busy code, not good. 129// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602 130// 131function pause(numberMillis) { 132 var now = new Date(); 133 var exitTime = now.getTime() + numberMillis; 134 while (true) { 135 now = new Date(); 136 if (now.getTime() > exitTime) 137 return; 138 } 139} 140 141// 142// getKey(key) 143// Gets keycode. If 'x' is pressed then it hides the lightbox. 144// 145 146function getKey(e){ 147 if (e == null) { // ie 148 keycode = event.keyCode; 149 } else { // mozilla 150 keycode = e.which; 151 } 152 key = String.fromCharCode(keycode).toLowerCase(); 153 154 if(key == 'x'){ hideLightbox(); } 155} 156 157 158// 159// listenKey() 160// 161function listenKey () { document.onkeypress = getKey; } 162 163 164// 165// showLightbox() 166// Preloads images. Pleaces new image in lightbox then centers and displays. 167// 168function showLightbox(objLink) 169{ 170 // prep objects 171 var objOverlay = document.getElementById('overlay'); 172 var objLightbox = document.getElementById('lightbox'); 173 var objCaption = document.getElementById('lightboxCaption'); 174 var objImage = document.getElementById('lightboxImage'); 175 var objLoadingImage = document.getElementById('loadingImage'); 176 var objLightboxDetails = document.getElementById('lightboxDetails'); 177 178 179 var arrayPageSize = getPageSize(); 180 var arrayPageScroll = getPageScroll(); 181 182 // center loadingImage if it exists 183 if (objLoadingImage) { 184 objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px'); 185 objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px'); 186 objLoadingImage.style.display = 'block'; 187 } 188 189 // set height of Overlay to take up whole page and show 190 objOverlay.style.height = (arrayPageSize[1] + 'px'); 191 objOverlay.style.display = 'block'; 192 193 // preload image 194 imgPreload = new Image(); 195 196 imgPreload.onload=function(){ 197 objImage.src = objLink.href; 198 199 // center lightbox and make sure that the top and left values are not negative 200 // and the image placed outside the viewport 201 var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2); 202 var lightboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2); 203 204 objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px"; 205 objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px"; 206 207 208 objLightboxDetails.style.width = imgPreload.width + 'px'; 209 210 if(objLink.getAttribute('title')){ 211 objCaption.style.display = 'block'; 212 //objCaption.style.width = imgPreload.width + 'px'; 213 objCaption.innerHTML = objLink.getAttribute('title'); 214 } else { 215 objCaption.style.display = 'none'; 216 } 217 218 // A small pause between the image loading and displaying is required with IE, 219 // this prevents the previous image displaying for a short burst causing flicker. 220 if (navigator.appVersion.indexOf("MSIE")!=-1){ 221 pause(250); 222 } 223 224 if (objLoadingImage) { objLoadingImage.style.display = 'none'; } 225 objLightbox.style.display = 'block'; 226 227 // After image is loaded, update the overlay height as the new image might have 228 // increased the overall page height. 229 arrayPageSize = getPageSize(); 230 objOverlay.style.height = (arrayPageSize[1] + 'px'); 231 232 // Check for 'x' keypress 233 listenKey(); 234 235 return false; 236 } 237 238 imgPreload.src = objLink.href; 239 240} 241 242 243 244 245 246// 247// hideLightbox() 248// 249function hideLightbox() 250{ 251 // get objects 252 objOverlay = document.getElementById('overlay'); 253 objLightbox = document.getElementById('lightbox'); 254 255 // hide lightbox and overlay 256 objOverlay.style.display = 'none'; 257 objLightbox.style.display = 'none'; 258 259 // disable keypress listener 260 document.onkeypress = ''; 261} 262 263 264 265 266// 267// initLightbox() 268// Function runs on window load, going through link tags looking for rel="lightbox". 269// These links receive onclick events that enable the lightbox display for their targets. 270// The function also inserts html markup at the top of the page which will be used as a 271// container for the overlay pattern and the inline image. 272// 273function initLightbox() 274{ 275 276 if (!document.getElementsByTagName){ return; } 277 var anchors = document.getElementsByTagName("a"); 278 279 // loop through all anchor tags 280 for (var i=0; i<anchors.length; i++){ 281 var anchor = anchors[i]; 282 283 if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox")){ 284 anchor.onclick = function () {showLightbox(this); return false;} 285 } 286 } 287 288 // the rest of this code inserts html at the top of the page that looks like this: 289 // 290 // <div id="overlay"> 291 // <a href="#" onclick="hideLightbox(); return false;"><img id="loadingImage" /></a> 292 // </div> 293 // <div id="lightbox"> 294 // <a href="#" onclick="hideLightbox(); return false;
294" title="Click anywhere to close image"> 295 // <img id="closeButton" /> 296 // <img id="lightboxImage" /> 297 // </a> 298 // <div id="lightboxDetails"> 299 // <div id="lightboxCaption"></div> 300 // <div id="keyboardMsg"></div> 301 // </div> 302 // </div> 303 304 var objBody = document.getElementsByTagName("body").item(0); 305 306 // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file) 307 var objOverlay = document.createElement("div"); 308 objOverlay.setAttribute('id','overlay'); 309 objOverlay.onclick = function () {hideLightbox(); return false;} 310 objOverlay.style.display = 'none'; 311 objOverlay.style.position = 'absolute'; 312 objOverlay.style.top = '0'; 313 objOverlay.style.left = '0'; 314 objOverlay.style.zIndex = '90'; 315 objOverlay.style.width = '100%'; 316 objBody.insertBefore(objOverlay, objBody.firstChild); 317 318 var arrayPageSize = getPageSize(); 319 var arrayPageScroll = getPageScroll(); 320 321 // preload and create loader image 322 var imgPreloader = new Image(); 323 324 // if loader image found, create link to hide lightbox and create loadingimage 325 imgPreloader.onload=function(){ 326 327 var objLoadingImageLink = document.createElement("a"); 328 objLoadingImageLink.setAttribute('href','#'); 329 objLoadingImageLink.onclick = function () {hideLightbox(); return false;} 330 objOverlay.appendChild(objLoadingImageLink); 331 332 var objLoadingImage = document.createElement("img"); 333 objLoadingImage.src = loadingImage; 334 objLoadingImage.setAttribute('id','loadingImage'); 335 objLoadingImage.style.position = 'absolute'; 336 objLoadingImage.style.zIndex = '150'; 337 objLoadingImageLink.appendChild(objLoadingImage); 338 339 imgPreloader.onload=function(){}; // clear onLoad, as IE will flip out w/animated gifs 340 341 return false; 342 } 343 344 imgPreloader.src = loadingImage; 345 346 // create lightbox div, same note about styles as above 347 var objLightbox = document.createElement("div"); 348 objLightbox.setAttribute('id','lightbox'); 349 objLightbox.style.display = 'none'; 350 objLightbox.style.position = 'absolute'; 351 objLightbox.style.zIndex = '100'; 352 objBody.insertBefore(objLightbox, objOverlay.nextSibling); 353 354 // create link 355 var objLink = document.createElement("a"); 356 objLink.setAttribute('href','#'); 357 objLink.setAttribute('title','Click to close'); 358 objLink.onclick = function () {hideLightbox(); return false;} 359 objLightbox.appendChild(objLink); 360 361 // preload and create close button image 362 var imgPreloadCloseButton = new Image(); 363 364 // if close button image found, 365 imgPreloadCloseButton.onload=function(){ 366 367 var objCloseButton = document.createElement("img"); 368 objCloseButton.src = closeButton; 369 objCloseButton.setAttribute('id','closeButton'); 370 objCloseButton.style.position = 'absolute'; 371 objCloseButton.style.zIndex = '200'; 372 objLink.appendChild(objCloseButton); 373 374 return false; 375 } 376 377 imgPreloadCloseButton.src = closeButton; 378 379 // create image 380 var objImage = document.createElement("img"); 381 objImage.setAttribute('id','lightboxImage'); 382 objLink.appendChild(objImage); 383 384 // create details div, a container for the caption and keyboard message 385 var objLightboxDetails = document.createElement("div"); 386 objLightboxDetails.setAttribute('id','lightboxDetails'); 387 objLightbox.appendChild(objLightboxDetails); 388 389 // create caption 390 var objCaption = document.createElement("div"); 391 objCaption.setAttribute('id','lightboxCaption'); 392 objCaption.style.display = 'none'; 393 objLightboxDetails.appendChild(objCaption); 394 395 // create keyboard message 396 var objKeyboardMsg = document.createElement("div"); 397 objKeyboardMsg.setAttribute('id','keyboardMsg'); 398 objKeyboardMsg.innerHTML = 'Press <kbd>x</kbd> to close.'; 399 objLightboxDetails.appendChild(objKeyboardMsg); 400 401 402} 403 404 405 406 407// 408// addLoadEvent() 409// Adds event to window.onload without overwriting currently assigned onload functions. 410// Function found at Simon Willison's weblog - http://simon.incutio.com/ 411// 412function addLoadEvent(func) 413{ 414 var oldonload = window.onload; 415 if (typeof window.onload != 'function'){ 416 window.onload = func; 417 } else { 418 window.onload = function(){ 419 oldonload(); 420 func(); 421 } 422 } 423 424} 425 426 427 428addLoadEvent(initLightbox); // run initLightbox onLoad
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.