Update version to 0.4

master
jfdelrio 2007-10-09 14:08:11 +00:00
parent d4d610beaa
commit d3e3bd5973
1 changed files with 356 additions and 363 deletions

View File

@ -1,364 +1,357 @@
/** /**
* jQuery Lightbox * jQuery Lightbox
* Version 0.3 - 09/17/2007 * Version 0.4 - 09/17/2007
* @author Warren Krewenki * @author Warren Krewenki
* *
* Based on Lightbox 2 by Lokesh Dhakar (http://www.huddletogether.com/projects/lightbox2/) * Based on Lightbox 2 by Lokesh Dhakar (http://www.huddletogether.com/projects/lightbox2/)
* Originally written to make use of the Prototype framework, and Script.acalo.us, now altered to use jQuery. * Originally written to make use of the Prototype framework, and Script.acalo.us, now altered to use jQuery.
* *
* New in this version: **/
* - fully encapsulated in one JSON object
* - new method of adding the lightbox html var Lightbox = {
* - removal of some dead functions fileLoadingImage : "admin/css/images/loading.gif",
**/ fileBottomNavCloseImage : 'admin/css/images/closelabel.gif',
overlayOpacity : 0.8,
var Lightbox = { borderSize : 10,
fileLoadingImage : "admin/css/images/loading.gif", imageArray : new Array,
fileBottomNavCloseImage : 'admin/css/images/closelabel.gif', activeImage : null,
overlayOpacity : 0.8, inprogress : false,
borderSize : 10, resizeSpeed : 350,
imageArray : new Array, destroyElement: function(id){
activeImage : null, if(el = document.getElementById(id)){
inprogress : false, el.parentNode.removeChild(el);
}
initialize: function() { },
// attach lightbox to any links with rel 'lightbox' initialize: function() {
$("a").each(function(){ // attach lightbox to any links with rel 'lightbox'
if(this.rel.toLowerCase().match('lightbox')){ $("a").each(function(){
$(this).click(function(){ if(this.rel.toLowerCase().match('lightbox')){
Lightbox.start(this); $(this).click(function(){
return false; Lightbox.start(this);
}); return false;
} });
}); }
});
$("body").append('<div id="overlay"></div><div id="lightbox"><div id="outerImageContainer"><div id="imageContainer"><img id="lightboxImage"><div style="" id="hoverNav"><a href="#" id="prevLink"></a><a href="#" id="nextLink"></a></div><div id="loading"><a href="#" id="loadingLink"><img src="admin/css/images/loading.gif"></a></div></div></div><div id="imageDataContainer"><div id="imageData"><div id="imageDetails"><span id="caption"></span><span id="numberDisplay"></span></div><div id="bottomNav"><a href="#" id="bottomNavClose"><img src="admin/css/images/close.gif"></a></div></div></div></div>');
$("#overlay").click(function(){ Lightbox.end(); }).hide(); Lightbox.destroyElement('overlay'); Lightbox.destroyElement('lightbox');
$("#lightbox").click(function(){ Lightbox.end();}).hide(); $("body").append('<div id="overlay"></div><div id="lightbox"><div id="outerImageContainer"><div id="imageContainer"><img id="lightboxImage"><div style="" id="hoverNav"><a href="#" id="prevLink"></a><a href="#" id="nextLink"></a></div><div id="loading"><a href="#" id="loadingLink"><img src="'+Lightbox.fileLoadingImage+'"></a></div></div></div><div id="imageDataContainer"><div id="imageData"><div id="imageDetails"><span id="caption"></span><span id="numberDisplay"></span></div><div id="bottomNav"><a href="#" id="bottomNavClose"><img src="'+Lightbox.fileBottomNavCloseImage+'"></a></div></div></div></div>');
$("#loadingLink").click(function(){ Lightbox.end(); return false;}); $("#overlay").click(function(){ Lightbox.end(); }).hide();
$("#bottomNavClose").click(function(){ Lightbox.end(); return false; }); $("#lightbox").click(function(){ Lightbox.end();}).hide();
$('#outerImageContainer').css({width: '250px', height: '250px;'}); $("#loadingLink").click(function(){ Lightbox.end(); return false;});
}, $("#bottomNavClose").click(function(){ Lightbox.end(); return false; });
$('#outerImageContainer').css({width: '250px', height: '250px;'});
// },
// start()
// Display overlay and lightbox. If image is part of a set, add siblings to Lightbox.imageArray. //
// // start()
start: function(imageLink) { // Display overlay and lightbox. If image is part of a set, add siblings to Lightbox.imageArray.
$("select, embed, object").hide(); //
start: function(imageLink) {
// * $("select, embed, object").hide();
var urlImg = $("img", imageLink).attr("src");
urlImg = urlImg.split("classes"); // stretch overlay to fill page and fade in
//urlImg = urlImg[0]+'admin/css/images/loading.gif'; var arrayPageSize = Lightbox.getPageSize();
$("#loadingLink > img").attr({src:urlImg[0]+'admin/css/images/loading.gif'}) $("#overlay").hide().css({width: '100%', height: arrayPageSize[1]+'px', opacity : Lightbox.overlayOpacity}).fadeIn();
$("#bottomNavClose > img").attr({src:urlImg[0]+'admin/css/images/close.gif'})
Lightbox.imageArray = [];
// stretch overlay to fill page and fade in imageNum = 0;
var arrayPageSize = Lightbox.getPageSize();
$("#overlay").hide().css({width: '100%', height: arrayPageSize[1]+'px', opacity : Lightbox.overlayOpacity}).fadeIn(); var anchors = document.getElementsByTagName( imageLink.tagName);
Lightbox.imageArray = []; // if image is NOT part of a set..
imageNum = 0; if((imageLink.rel == 'lightbox')){
// add single image to Lightbox.imageArray
var anchors = document.getElementsByTagName( imageLink.tagName); Lightbox.imageArray.push(new Array(imageLink.href, imageLink.title));
} else {
// if image is NOT part of a set.. // if image is part of a set..
if((imageLink.rel == 'lightbox')){
// add single image to Lightbox.imageArray // loop through anchors, find other images in set, and add them to Lightbox.imageArray
Lightbox.imageArray.push(new Array(imageLink.href, imageLink.title)); for (var i=0; i<anchors.length; i++){
} else { var anchor = anchors[i];
// if image is part of a set.. if (anchor.href && (anchor.rel == imageLink.rel)){
Lightbox.imageArray.push(new Array(anchor.href, anchor.title));
// loop through anchors, find other images in set, and add them to Lightbox.imageArray }
for (var i=0; i<anchors.length; i++){ }
var anchor = anchors[i];
if (anchor.href && (anchor.rel == imageLink.rel)){ for(i = 0; i < Lightbox.imageArray.length; i++){
Lightbox.imageArray.push(new Array(anchor.href, anchor.title)); for(j = Lightbox.imageArray.length-1; j>i; j--){
} if(Lightbox.imageArray[i][0] == Lightbox.imageArray[j][0]){
} Lightbox.imageArray.splice(j,1);
}
for(i = 0; i < Lightbox.imageArray.length; i++){ }
for(j = Lightbox.imageArray.length-1; j>i; j--){ }
if(Lightbox.imageArray[i][0] == Lightbox.imageArray[j][0]){ while(Lightbox.imageArray[imageNum][0] != imageLink.href) { imageNum++;}
Lightbox.imageArray.splice(j,1); }
}
} // calculate top and left offset for the lightbox
} var arrayPageScroll = Lightbox.getPageScroll();
while(Lightbox.imageArray[imageNum][0] != imageLink.href) { imageNum++;} var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
} var lightboxLeft = arrayPageScroll[0];
$('#lightbox').css({top: lightboxTop+'px', left: lightboxLeft+'px'}).show();
// calculate top and left offset for the lightbox
var arrayPageScroll = Lightbox.getPageScroll(); this.changeImage(imageNum);
var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10); },
var lightboxLeft = arrayPageScroll[0];
$('#lightbox').css({top: lightboxTop+'px', left: lightboxLeft+'px'}).show(); //
// changeImage()
this.changeImage(imageNum); // Hide most elements and preload image in preparation for resizing image container.
}, //
changeImage: function(imageNum) {
// if(this.inprogress == false){
// changeImage() this.inprogress = true;
// Hide most elements and preload image in preparation for resizing image container. Lightbox.activeImage = imageNum; // update global var
//
changeImage: function(imageNum) { // hide elements during transition
if(this.inprogress == false){ $('#loading').show();
this.inprogress = true; $('#lightboxImage').hide();
Lightbox.activeImage = imageNum; // update global var $('#hoverNav').hide();
$('#prevLink').hide();
// hide elements during transition $('#nextLink').hide();
$('#loading').show(); $('#imageDataContainer').hide();
$('#lightboxImage').hide(); $('#numberDisplay').hide();
$('#hoverNav').hide();
$('#prevLink').hide(); imgPreloader = new Image();
$('#nextLink').hide();
$('#imageDataContainer').hide(); // once image is preloaded, resize image container
$('#numberDisplay').hide(); imgPreloader.onload=function(){
document.getElementById('lightboxImage').src = Lightbox.imageArray[Lightbox.activeImage][0];
imgPreloader = new Image(); Lightbox.resizeImageContainer(imgPreloader.width, imgPreloader.height);
}
// once image is preloaded, resize image container imgPreloader.src = Lightbox.imageArray[Lightbox.activeImage][0];
imgPreloader.onload=function(){ }
console.log(Lightbox.activeImage); },
document.getElementById('lightboxImage').src = Lightbox.imageArray[Lightbox.activeImage][0];
Lightbox.resizeImageContainer(imgPreloader.width, imgPreloader.height); //
} // resizeImageContainer()
imgPreloader.src = Lightbox.imageArray[Lightbox.activeImage][0]; //
} resizeImageContainer: function( imgWidth, imgHeight) {
},
// get curren width and height
// this.widthCurrent = document.getElementById('outerImageContainer').offsetWidth;
// resizeImageContainer() this.heightCurrent = document.getElementById('outerImageContainer').offsetHeight;
//
resizeImageContainer: function( imgWidth, imgHeight) { // get new width and height
var widthNew = (imgWidth + (Lightbox.borderSize * 2));
// get curren width and height var heightNew = (imgHeight + (Lightbox.borderSize * 2));
this.widthCurrent = document.getElementById('outerImageContainer').offsetWidth;
this.heightCurrent = document.getElementById('outerImageContainer').offsetHeight; // scalars based on change from old to new
this.xScale = ( widthNew / this.widthCurrent) * 100;
// get new width and height this.yScale = ( heightNew / this.heightCurrent) * 100;
var widthNew = (imgWidth + (Lightbox.borderSize * 2));
var heightNew = (imgHeight + (Lightbox.borderSize * 2)); // calculate size difference between new and old image, and resize if necessary
wDiff = this.widthCurrent - widthNew;
// scalars based on change from old to new hDiff = this.heightCurrent - heightNew;
this.xScale = ( widthNew / this.widthCurrent) * 100;
this.yScale = ( heightNew / this.heightCurrent) * 100; $('#outerImageContainer').animate({width: widthNew, height: heightNew},Lightbox.resizeSpeed,'linear',function(){
Lightbox.showImage();
// calculate size difference between new and old image, and resize if necessary
wDiff = this.widthCurrent - widthNew; });
hDiff = this.heightCurrent - heightNew;
$('#outerImageContainer').animate({width: widthNew},400,'linear',function(){ // if new and old image are same size and no scaling transition is necessary,
$("#outerImageContainer").animate({height: heightNew},400,'linear',function(){ // do a quick pause to prevent image flicker.
Lightbox.showImage(); if((hDiff == 0) && (wDiff == 0)){
}); if (navigator.appVersion.indexOf("MSIE")!=-1){ Lightbox.pause(250); } else { Lightbox.pause(100);}
}); }
$('#prevLink').css({height: imgHeight+'px'});
// if new and old image are same size and no scaling transition is necessary, $('#nextLink').css({height: imgHeight+'px'});
// do a quick pause to prevent image flicker. $('#imageDataContainer').css({width: widthNew+'px'});
if((hDiff == 0) && (wDiff == 0)){
if (navigator.appVersion.indexOf("MSIE")!=-1){ Lightbox.pause(250); } else { Lightbox.pause(100);}
} },
$('#prevLink').css({height: imgHeight+'px'}); //
$('#nextLink').css({height: imgHeight+'px'}); // showImage()
$('#imageDataContainer').css({width: widthNew+'px'}); // Display image and begin preloading neighbors.
//
showImage: function(){
}, $('#loading').hide();
$('#lightboxImage').fadeIn("fast");
// Lightbox.updateDetails();
// showImage() this.preloadNeighborImages();
// Display image and begin preloading neighbors. this.inprogress = false;
// },
showImage: function(){
$('#loading').hide(); //
$('#lightboxImage').fadeIn("fast"); // updateDetails()
Lightbox.updateDetails(); // Display caption, image number, and bottom nav.
this.preloadNeighborImages(); //
this.inprogress = false; updateDetails: function() {
}, $("#imageDataContainer").hide();
// if caption is not null
// if(Lightbox.imageArray[Lightbox.activeImage][1]){
// updateDetails() $('#caption').html(Lightbox.imageArray[Lightbox.activeImage][1]).show();
// Display caption, image number, and bottom nav. }
//
updateDetails: function() { // if image is part of set display 'Image x of x'
$("#imageDataContainer").hide(); if(Lightbox.imageArray.length > 1){
// if caption is not null $('#numberDisplay').html("Image " + eval(Lightbox.activeImage + 1) + " of " + Lightbox.imageArray.length).show();
if(Lightbox.imageArray[Lightbox.activeImage][1]){ }
$('#caption').html(Lightbox.imageArray[Lightbox.activeImage][1]).show();
} $("#imageDataContainer").hide().slideDown("slow");
var arrayPageSize = Lightbox.getPageSize();
// if image is part of set display 'Image x of x' $('#overLay').css({height: arrayPageSize[1]+'px'});
if(Lightbox.imageArray.length > 1){ Lightbox.updateNav();
$('#numberDisplay').html("Image " + eval(Lightbox.activeImage + 1) + " of " + Lightbox.imageArray.length).show(); },
}
//
$("#imageDataContainer").hide().slideDown("slow"); // updateNav()
var arrayPageSize = Lightbox.getPageSize(); // Display appropriate previous and next hover navigation.
$('#overLay').css({height: arrayPageSize[1]+'px'}); //
Lightbox.updateNav(); updateNav: function() {
},
$('#hoverNav').show();
//
// updateNav() // if not first image in set, display prev image button
// Display appropriate previous and next hover navigation. if(Lightbox.activeImage != 0){
// $('#prevLink').show().click(function(){
updateNav: function() { Lightbox.changeImage(Lightbox.activeImage - 1); return false;
});
$('#hoverNav').show(); }
// if not first image in set, display prev image button // if not last image in set, display next image button
if(Lightbox.activeImage != 0){ if(Lightbox.activeImage != (Lightbox.imageArray.length - 1)){
$('#prevLink').show().click(function(){ $('#nextLink').show().click(function(){
Lightbox.changeImage(Lightbox.activeImage - 1); return false;
}); Lightbox.changeImage(Lightbox.activeImage +1); return false;
} });
}
// if not last image in set, display next image button
if(Lightbox.activeImage != (Lightbox.imageArray.length - 1)){ this.enableKeyboardNav();
$('#nextLink').show().click(function(){ },
Lightbox.changeImage(Lightbox.activeImage +1); return false;
}); enableKeyboardNav: function() {
} document.onkeydown = this.keyboardAction;
},
this.enableKeyboardNav();
}, disableKeyboardNav: function() {
document.onkeydown = '';
},
enableKeyboardNav: function() {
document.onkeydown = this.keyboardAction; keyboardAction: function(e) {
}, if (e == null) { // ie
keycode = event.keyCode;
disableKeyboardNav: function() { escapeKey = 27;
document.onkeydown = ''; } else { // mozilla
}, keycode = e.keyCode;
escapeKey = e.DOM_VK_ESCAPE;
keyboardAction: function(e) { }
if (e == null) { // ie
keycode = event.keyCode; key = String.fromCharCode(keycode).toLowerCase();
escapeKey = 27;
} else { // mozilla if((key == 'x') || (key == 'o') || (key == 'c') || (keycode == escapeKey)){ // close lightbox
keycode = e.keyCode; Lightbox.end();
escapeKey = e.DOM_VK_ESCAPE; } else if((key == 'p') || (keycode == 37)){ // display previous image
} if(Lightbox.activeImage != 0){
Lightbox.disableKeyboardNav();
key = String.fromCharCode(keycode).toLowerCase(); Lightbox.changeImage(Lightbox.activeImage - 1);
}
if((key == 'x') || (key == 'o') || (key == 'c') || (keycode == escapeKey)){ // close lightbox } else if((key == 'n') || (keycode == 39)){ // display next image
Lightbox.end(); if(Lightbox.activeImage != (Lightbox.imageArray.length - 1)){
} else if((key == 'p') || (keycode == 37)){ // display previous image Lightbox.disableKeyboardNav();
if(Lightbox.activeImage != 0){ Lightbox.changeImage(Lightbox.activeImage + 1);
Lightbox.disableKeyboardNav(); }
Lightbox.changeImage(Lightbox.activeImage - 1); }
}
} else if((key == 'n') || (keycode == 39)){ // display next image },
if(Lightbox.activeImage != (Lightbox.imageArray.length - 1)){
Lightbox.disableKeyboardNav(); preloadNeighborImages: function(){
Lightbox.changeImage(Lightbox.activeImage + 1);
} if((Lightbox.imageArray.length - 1) > Lightbox.activeImage){
} preloadNextImage = new Image();
preloadNextImage.src = Lightbox.imageArray[Lightbox.activeImage + 1][0];
}, }
if(Lightbox.activeImage > 0){
preloadNeighborImages: function(){ preloadPrevImage = new Image();
preloadPrevImage.src = Lightbox.imageArray[Lightbox.activeImage - 1][0];
if((Lightbox.imageArray.length - 1) > Lightbox.activeImage){ }
preloadNextImage = new Image();
preloadNextImage.src = Lightbox.imageArray[Lightbox.activeImage + 1][0]; },
}
if(Lightbox.activeImage > 0){ end: function() {
preloadPrevImage = new Image(); this.disableKeyboardNav();
preloadPrevImage.src = Lightbox.imageArray[Lightbox.activeImage - 1][0]; $('#lightbox').hide();
} $("#overlay").fadeOut();
$("select, object, embed").show();
}, },
end: function() { getPageSize : function(){
this.disableKeyboardNav(); var xScroll, yScroll;
$('#lightbox').hide();
$("#overlay").fadeOut(); if (window.innerHeight && window.scrollMaxY) {
$("select, object, embed").show(); xScroll = window.innerWidth + window.scrollMaxX;
}, yScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
getPageSize : function(){ xScroll = document.body.scrollWidth;
var xScroll, yScroll; yScroll = document.body.scrollHeight;
} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
if (window.innerHeight && window.scrollMaxY) { xScroll = document.body.offsetWidth;
xScroll = window.innerWidth + window.scrollMaxX; yScroll = document.body.offsetHeight;
yScroll = window.innerHeight + window.scrollMaxY; }
} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
xScroll = document.body.scrollWidth; var windowWidth, windowHeight;
yScroll = document.body.scrollHeight;
} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari if (self.innerHeight) { // all except Explorer
xScroll = document.body.offsetWidth; if(document.documentElement.clientWidth){
yScroll = document.body.offsetHeight; windowWidth = document.documentElement.clientWidth;
} } else {
windowWidth = self.innerWidth;
var windowWidth, windowHeight; }
windowHeight = self.innerHeight;
if (self.innerHeight) { // all except Explorer } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
if(document.documentElement.clientWidth){ windowWidth = document.documentElement.clientWidth;
windowWidth = document.documentElement.clientWidth; windowHeight = document.documentElement.clientHeight;
} else { } else if (document.body) { // other Explorers
windowWidth = self.innerWidth; windowWidth = document.body.clientWidth;
} windowHeight = document.body.clientHeight;
windowHeight = self.innerHeight; }
} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
windowWidth = document.documentElement.clientWidth; // for small pages with total height less then height of the viewport
windowHeight = document.documentElement.clientHeight; if(yScroll < windowHeight){
} else if (document.body) { // other Explorers pageHeight = windowHeight;
windowWidth = document.body.clientWidth; } else {
windowHeight = document.body.clientHeight; pageHeight = yScroll;
} }
// for small pages with total height less then height of the viewport
if(yScroll < windowHeight){ // for small pages with total width less then width of the viewport
pageHeight = windowHeight; if(xScroll < windowWidth){
} else { pageWidth = xScroll;
pageHeight = yScroll; } else {
} pageWidth = windowWidth;
}
// for small pages with total width less then width of the viewport arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
if(xScroll < windowWidth){ return arrayPageSize;
pageWidth = xScroll; },
} else { getPageScroll : function(){
pageWidth = windowWidth;
} var xScroll, yScroll;
arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) if (self.pageYOffset) {
return arrayPageSize; yScroll = self.pageYOffset;
}, xScroll = self.pageXOffset;
getPageScroll : function(){ } else if (document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict
yScroll = document.documentElement.scrollTop;
var xScroll, yScroll; xScroll = document.documentElement.scrollLeft;
} else if (document.body) {// all other Explorers
if (self.pageYOffset) { yScroll = document.body.scrollTop;
yScroll = self.pageYOffset; xScroll = document.body.scrollLeft;
xScroll = self.pageXOffset; }
} else if (document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict
yScroll = document.documentElement.scrollTop; arrayPageScroll = new Array(xScroll,yScroll)
xScroll = document.documentElement.scrollLeft; return arrayPageScroll;
} else if (document.body) {// all other Explorers },
yScroll = document.body.scrollTop; pause : function(ms){
xScroll = document.body.scrollLeft; var date = new Date();
} curDate = null;
do{var curDate = new Date();}
arrayPageScroll = new Array(xScroll,yScroll) while( curDate - date < ms);
return arrayPageScroll; }
}, };
pause : function(ms){
var date = new Date(); $(document).ready(function(){
curDate = null; Lightbox.initialize();
do{var curDate = new Date();}
while( curDate - date < ms);
}
};
$(document).ready(function(){
Lightbox.initialize();
}); });