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