A tumblelog CMS built on AJAX, PHP and MySQL.

lightbox.js 11KB

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