A tumblelog CMS built on AJAX, PHP and MySQL.

lightbox.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /**
  2. * jQuery Lightbox
  3. * Version 0.3 - 09/17/2007
  4. * @author Warren Krewenki
  5. *
  6. * Based on Lightbox 2 by Lokesh Dhakar (http://www.huddletogether.com/projects/lightbox2/)
  7. * Originally written to make use of the Prototype framework, and Script.acalo.us, now altered to use jQuery.
  8. *
  9. * New in this version:
  10. * - fully encapsulated in one JSON object
  11. * - new method of adding the lightbox html
  12. * - removal of some dead functions
  13. **/
  14. var Lightbox = {
  15. fileLoadingImage : "admin/css/images/loading.gif",
  16. fileBottomNavCloseImage : 'admin/css/images/closelabel.gif',
  17. overlayOpacity : 0.8,
  18. borderSize : 10,
  19. imageArray : new Array,
  20. activeImage : null,
  21. inprogress : false,
  22. initialize: function() {
  23. // attach lightbox to any links with rel 'lightbox'
  24. $("a").each(function(){
  25. if(this.rel.toLowerCase().match('lightbox')){
  26. $(this).click(function(){
  27. Lightbox.start(this);
  28. return false;
  29. });
  30. }
  31. });
  32. $("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>');
  33. $("#overlay").click(function(){ Lightbox.end(); }).hide();
  34. $("#lightbox").click(function(){ Lightbox.end();}).hide();
  35. $("#loadingLink").click(function(){ Lightbox.end(); return false;});
  36. $("#bottomNavClose").click(function(){ Lightbox.end(); return false; });
  37. $('#outerImageContainer').css({width: '250px', height: '250px;'});
  38. },
  39. //
  40. // start()
  41. // Display overlay and lightbox. If image is part of a set, add siblings to Lightbox.imageArray.
  42. //
  43. start: function(imageLink) {
  44. $("select, embed, object").hide();
  45. // *
  46. var urlImg = $("img", imageLink).attr("src");
  47. urlImg = urlImg.split("classes");
  48. //urlImg = urlImg[0]+'admin/css/images/loading.gif';
  49. $("#loadingLink > img").attr({src:urlImg[0]+'admin/css/images/loading.gif'})
  50. $("#bottomNavClose > img").attr({src:urlImg[0]+'admin/css/images/close.gif'})
  51. // stretch overlay to fill page and fade in
  52. var arrayPageSize = Lightbox.getPageSize();
  53. $("#overlay").hide().css({width: '100%', height: arrayPageSize[1]+'px', opacity : Lightbox.overlayOpacity}).fadeIn();
  54. Lightbox.imageArray = [];
  55. imageNum = 0;
  56. var anchors = document.getElementsByTagName( imageLink.tagName);
  57. // if image is NOT part of a set..
  58. if((imageLink.rel == 'lightbox')){
  59. // add single image to Lightbox.imageArray
  60. Lightbox.imageArray.push(new Array(imageLink.href, imageLink.title));
  61. } else {
  62. // if image is part of a set..
  63. // loop through anchors, find other images in set, and add them to Lightbox.imageArray
  64. for (var i=0; i<anchors.length; i++){
  65. var anchor = anchors[i];
  66. if (anchor.href && (anchor.rel == imageLink.rel)){
  67. Lightbox.imageArray.push(new Array(anchor.href, anchor.title));
  68. }
  69. }
  70. for(i = 0; i < Lightbox.imageArray.length; i++){
  71. for(j = Lightbox.imageArray.length-1; j>i; j--){
  72. if(Lightbox.imageArray[i][0] == Lightbox.imageArray[j][0]){
  73. Lightbox.imageArray.splice(j,1);
  74. }
  75. }
  76. }
  77. while(Lightbox.imageArray[imageNum][0] != imageLink.href) { imageNum++;}
  78. }
  79. // calculate top and left offset for the lightbox
  80. var arrayPageScroll = Lightbox.getPageScroll();
  81. var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
  82. var lightboxLeft = arrayPageScroll[0];
  83. $('#lightbox').css({top: lightboxTop+'px', left: lightboxLeft+'px'}).show();
  84. this.changeImage(imageNum);
  85. },
  86. //
  87. // changeImage()
  88. // Hide most elements and preload image in preparation for resizing image container.
  89. //
  90. changeImage: function(imageNum) {
  91. if(this.inprogress == false){
  92. this.inprogress = true;
  93. Lightbox.activeImage = imageNum; // update global var
  94. // hide elements during transition
  95. $('#loading').show();
  96. $('#lightboxImage').hide();
  97. $('#hoverNav').hide();
  98. $('#prevLink').hide();
  99. $('#nextLink').hide();
  100. $('#imageDataContainer').hide();
  101. $('#numberDisplay').hide();
  102. imgPreloader = new Image();
  103. // once image is preloaded, resize image container
  104. imgPreloader.onload=function(){
  105. console.log(Lightbox.activeImage);
  106. document.getElementById('lightboxImage').src = Lightbox.imageArray[Lightbox.activeImage][0];
  107. Lightbox.resizeImageContainer(imgPreloader.width, imgPreloader.height);
  108. }
  109. imgPreloader.src = Lightbox.imageArray[Lightbox.activeImage][0];
  110. }
  111. },
  112. //
  113. // resizeImageContainer()
  114. //
  115. resizeImageContainer: function( imgWidth, imgHeight) {
  116. // get curren width and height
  117. this.widthCurrent = document.getElementById('outerImageContainer').offsetWidth;
  118. this.heightCurrent = document.getElementById('outerImageContainer').offsetHeight;
  119. // get new width and height
  120. var widthNew = (imgWidth + (Lightbox.borderSize * 2));
  121. var heightNew = (imgHeight + (Lightbox.borderSize * 2));
  122. // scalars based on change from old to new
  123. this.xScale = ( widthNew / this.widthCurrent) * 100;
  124. this.yScale = ( heightNew / this.heightCurrent) * 100;
  125. // calculate size difference between new and old image, and resize if necessary
  126. wDiff = this.widthCurrent - widthNew;
  127. hDiff = this.heightCurrent - heightNew;
  128. $('#outerImageContainer').animate({width: widthNew},400,'linear',function(){
  129. $("#outerImageContainer").animate({height: heightNew},400,'linear',function(){
  130. Lightbox.showImage();
  131. });
  132. });
  133. // if new and old image are same size and no scaling transition is necessary,
  134. // do a quick pause to prevent image flicker.
  135. if((hDiff == 0) && (wDiff == 0)){
  136. if (navigator.appVersion.indexOf("MSIE")!=-1){ Lightbox.pause(250); } else { Lightbox.pause(100);}
  137. }
  138. $('#prevLink').css({height: imgHeight+'px'});
  139. $('#nextLink').css({height: imgHeight+'px'});
  140. $('#imageDataContainer').css({width: widthNew+'px'});
  141. },
  142. //
  143. // showImage()
  144. // Display image and begin preloading neighbors.
  145. //
  146. showImage: function(){
  147. $('#loading').hide();
  148. $('#lightboxImage').fadeIn("fast");
  149. Lightbox.updateDetails();
  150. this.preloadNeighborImages();
  151. this.inprogress = false;
  152. },
  153. //
  154. // updateDetails()
  155. // Display caption, image number, and bottom nav.
  156. //
  157. updateDetails: function() {
  158. $("#imageDataContainer").hide();
  159. // if caption is not null
  160. if(Lightbox.imageArray[Lightbox.activeImage][1]){
  161. $('#caption').html(Lightbox.imageArray[Lightbox.activeImage][1]).show();
  162. }
  163. // if image is part of set display 'Image x of x'
  164. if(Lightbox.imageArray.length > 1){
  165. $('#numberDisplay').html("Image " + eval(Lightbox.activeImage + 1) + " of " + Lightbox.imageArray.length).show();
  166. }
  167. $("#imageDataContainer").hide().slideDown("slow");
  168. var arrayPageSize = Lightbox.getPageSize();
  169. $('#overLay').css({height: arrayPageSize[1]+'px'});
  170. Lightbox.updateNav();
  171. },
  172. //
  173. // updateNav()
  174. // Display appropriate previous and next hover navigation.
  175. //
  176. updateNav: function() {
  177. $('#hoverNav').show();
  178. // if not first image in set, display prev image button
  179. if(Lightbox.activeImage != 0){
  180. $('#prevLink').show().click(function(){
  181. Lightbox.changeImage(Lightbox.activeImage - 1); return false;
  182. });
  183. }
  184. // if not last image in set, display next image button
  185. if(Lightbox.activeImage != (Lightbox.imageArray.length - 1)){
  186. $('#nextLink').show().click(function(){
  187. Lightbox.changeImage(Lightbox.activeImage +1); return false;
  188. });
  189. }
  190. this.enableKeyboardNav();
  191. },
  192. enableKeyboardNav: function() {
  193. document.onkeydown = this.keyboardAction;
  194. },
  195. disableKeyboardNav: function() {
  196. document.onkeydown = '';
  197. },
  198. keyboardAction: function(e) {
  199. if (e == null) { // ie
  200. keycode = event.keyCode;
  201. escapeKey = 27;
  202. } else { // mozilla
  203. keycode = e.keyCode;
  204. escapeKey = e.DOM_VK_ESCAPE;
  205. }
  206. key = String.fromCharCode(keycode).toLowerCase();
  207. if((key == 'x') || (key == 'o') || (key == 'c') || (keycode == escapeKey)){ // close lightbox
  208. Lightbox.end();
  209. } else if((key == 'p') || (keycode == 37)){ // display previous image
  210. if(Lightbox.activeImage != 0){
  211. Lightbox.disableKeyboardNav();
  212. Lightbox.changeImage(Lightbox.activeImage - 1);
  213. }
  214. } else if((key == 'n') || (keycode == 39)){ // display next image
  215. if(Lightbox.activeImage != (Lightbox.imageArray.length - 1)){
  216. Lightbox.disableKeyboardNav();
  217. Lightbox.changeImage(Lightbox.activeImage + 1);
  218. }
  219. }
  220. },
  221. preloadNeighborImages: function(){
  222. if((Lightbox.imageArray.length - 1) > Lightbox.activeImage){
  223. preloadNextImage = new Image();
  224. preloadNextImage.src = Lightbox.imageArray[Lightbox.activeImage + 1][0];
  225. }
  226. if(Lightbox.activeImage > 0){
  227. preloadPrevImage = new Image();
  228. preloadPrevImage.src = Lightbox.imageArray[Lightbox.activeImage - 1][0];
  229. }
  230. },
  231. end: function() {
  232. this.disableKeyboardNav();
  233. $('#lightbox').hide();
  234. $("#overlay").fadeOut();
  235. $("select, object, embed").show();
  236. },
  237. getPageSize : function(){
  238. var xScroll, yScroll;
  239. if (window.innerHeight && window.scrollMaxY) {
  240. xScroll = window.innerWidth + window.scrollMaxX;
  241. yScroll = window.innerHeight + window.scrollMaxY;
  242. } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
  243. xScroll = document.body.scrollWidth;
  244. yScroll = document.body.scrollHeight;
  245. } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
  246. xScroll = document.body.offsetWidth;
  247. yScroll = document.body.offsetHeight;
  248. }
  249. var windowWidth, windowHeight;
  250. if (self.innerHeight) { // all except Explorer
  251. if(document.documentElement.clientWidth){
  252. windowWidth = document.documentElement.clientWidth;
  253. } else {
  254. windowWidth = self.innerWidth;
  255. }
  256. windowHeight = self.innerHeight;
  257. } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
  258. windowWidth = document.documentElement.clientWidth;
  259. windowHeight = document.documentElement.clientHeight;
  260. } else if (document.body) { // other Explorers
  261. windowWidth = document.body.clientWidth;
  262. windowHeight = document.body.clientHeight;
  263. }
  264. // for small pages with total height less then height of the viewport
  265. if(yScroll < windowHeight){
  266. pageHeight = windowHeight;
  267. } else {
  268. pageHeight = yScroll;
  269. }
  270. // for small pages with total width less then width of the viewport
  271. if(xScroll < windowWidth){
  272. pageWidth = xScroll;
  273. } else {
  274. pageWidth = windowWidth;
  275. }
  276. arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
  277. return arrayPageSize;
  278. },
  279. getPageScroll : function(){
  280. var xScroll, yScroll;
  281. if (self.pageYOffset) {
  282. yScroll = self.pageYOffset;
  283. xScroll = self.pageXOffset;
  284. } else if (document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict
  285. yScroll = document.documentElement.scrollTop;
  286. xScroll = document.documentElement.scrollLeft;
  287. } else if (document.body) {// all other Explorers
  288. yScroll = document.body.scrollTop;
  289. xScroll = document.body.scrollLeft;
  290. }
  291. arrayPageScroll = new Array(xScroll,yScroll)
  292. return arrayPageScroll;
  293. },
  294. pause : function(ms){
  295. var date = new Date();
  296. curDate = null;
  297. do{var curDate = new Date();}
  298. while( curDate - date < ms);
  299. }
  300. };
  301. $(document).ready(function(){
  302. Lightbox.initialize();
  303. });