simplelightbox.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * simplelightbox.js - A minimal, unobtrusive javascript lightbox engine for
  3. * easily highlighting images in web pages (no libraries required)
  4. *
  5. * This program is free software - you are free to use it however you want.
  6. * For more information, look up the GNU GPL.
  7. *
  8. * Authored by Vitor Sonoki - http://sonokisworld.appspot.com
  9. *
  10. * version: 0.8
  11. */
  12. 'use strict';
  13. // This global variable counts all the pictures that
  14. var index = 0;
  15. // This is a class that applies the functionality to the images in this page.
  16. function Picture(image) {
  17. image.onclick = function() {
  18. showHighlights(image);
  19. }
  20. image.style.cursor = "pointer";
  21. // set a Token to state which picture index this is.
  22. image.index = index;
  23. index++;
  24. }
  25. function showHighlights(image) {
  26. // Counting token:
  27. var index = image.index
  28. var realestate = {
  29. width: window.innerWidth,
  30. height: window.innerHeight
  31. };
  32. var shade = document.createElement("div");
  33. shade.style.background = "rgba(0,0,0,0.8)";
  34. shade.style.zIndex = "97";
  35. shade.style.position = "fixed";
  36. shade.style.top = "0px";
  37. shade.style.left = "0px";
  38. shade.style.width = "100%";
  39. shade.style.height = "100%";
  40. // Changed on v0.7: now we're packaging a counter saying how many more
  41. // pictures are left (ex: 1/4, 3/15, 2/5...)
  42. var imgCounter = document.createElement('div');
  43. // Changed on v0.8: we will include a counter and a legend extracted from "alt"
  44. // counter goes on top of the picture, legend on the bottom.
  45. var legend = document.createElement('div');
  46. imgCounter.style.fontSize = legend.style.fontSize = "20px";
  47. imgCounter.style.width = legend.style.width = "100%";
  48. imgCounter.style.zIndex = legend.style.zIndex = "98";
  49. imgCounter.style.textAlign = legend.style.textAlign = "center";
  50. imgCounter.style.position = legend.style.position = "fixed";
  51. imgCounter.style.color = legend.style.color = "#FFF";
  52. imgCounter.style.left = legend.style.left = "0";
  53. // The picture we're currently showing:
  54. var highlight = document.createElement('img');
  55. highlight.src = image.src;
  56. highlight.style.maxHeight = "84%";
  57. highlight.style.maxWidth = "92%";
  58. highlight.style.zIndex = "99";
  59. highlight.style.position = "fixed";
  60. highlight.style.margin = highlight.style.padding = "0";
  61. highlight.style.border = "4px solid white";
  62. highlight.style.display = "block";
  63. if (!(window.index === 0)) {
  64. highlight.style.cursor = "pointer";
  65. }
  66. // Base64 for maximum portability!
  67. var closebutton = document.createElement('div');
  68. closebutton.style.width = "30px";
  69. closebutton.style.height = "30px";
  70. closebutton.style.backgroundImage = "url('data:image/png; base64, iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAAAXNSR0IArs4c6QAAAAZiS0dEALwAGgAawKeBtgAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB90LDgAzEBynlicAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAADKElEQVRIx8WXTUgbURCAv7dNav1FwcZDFZsItmBugnSDIIigBw/m4MWeWqWXFoVAKUh7sggi5FDaS8H2VK/2kEMDIgiSiGBPBlrBBKkFsxQsqcZaQ18P7i6bdVdj/zK395id782beTOzghJFSjkE9AGdQBtwFSgAe8AWsA4sCiHe8qcipayRUk5JKbOydMnq39T8LvTOBYFOB7jjZl+4QJ8D9617+XSabCzG12SSg81NjnZ3ER4PlxsbqW5vp15VaRocpCoQsJt7IYR4cC5YSvkGGLEC09Eo2YWFkm6qKRwmEInYDzAvhLjtCrZ7+nl+no+Tk8jj4wuFSXi93Jie5trIiKvnwhpT4JWxzjx7RnpmBk9dHQCFXO7CeRJ49Aj/+Lh1664Q4rUJ1jNwC/AZnn54+JCajg6u9vdzpbmZT3Nz7KdS58LqVZWvyaS5vjk7a/VcA9qEEPsGeAp4bMR0tbeXS5WVtIyNcX1iAsXj4UjTWO3pOdPzrnic2mCQvUSC98PD5rXfWlqyxvypEOKJoi/uGbvpaNSM6ZXmZhSPB4AKn4+ud+9coerKCrXBIAANoRD1qnoSwuNj0tGoVfUegKJXJJ/5ZPTsLeRyfJqb40jTzC8qW1tRV1YcoVV+v7n+trFRdN3ZhQXy6bSx9EkphxS9DJ4oxGJFBvdTKVZ7ejjc3jb3qvz+Irgdms9kWOvvP3U4m+0+Ra+9AEWnNKSQy5EIhchnMqfgTtBkd7djKGy2OxW94ANwsLnpGsNkd/cpeKlQB9ttCtBgrH58+XLmU7HDS4U62G5QKJMoej8F4HJj45nK9pi6JZyT2GzvKXrFAqC6vb1kaD6TcUw4N7HZ3lL0ycEsd3bx1NURSiQcE8kp4dzgNtvrCrBotrTBwSLlmo4Obi0vU9na6ppIpcJtthcVfUbSAKoCAZrCYdPTltFRKnw+U/twe9sxe53gXfF4UY+21GpNCPHWyOqXZiuLRBBeLwDfd3b4WSgAcKRprA0MlPzUaoNB6lUV4fUSiESsqi//SVu0d6gz2+LfHgSMnnzuIFDW0aesw97/HG/LNtCX7RdGlOun7RfiVqZ/N/SVbwAAAABJRU5ErkJggg==')";
  71. closebutton.style.position = "fixed";
  72. closebutton.style.zIndex = "100";
  73. closebutton.style.cursor = "pointer";
  74. document.body.appendChild(shade);
  75. document.body.appendChild(highlight);
  76. document.body.appendChild(closebutton);
  77. document.body.appendChild(imgCounter);
  78. document.body.appendChild(legend);
  79. // get full dimensions of the pictures:
  80. var dimensions = {
  81. width: highlight.clientWidth,
  82. height: highlight.clientHeight
  83. };
  84. // Positioning like a bawce
  85. highlight.style.top = (((realestate.height - dimensions.height) / 2) - 15).toString() + "px";
  86. highlight.style.left = ((realestate.width - dimensions.width) / 2).toString() + "px";
  87. imgCounter.style.top = (parseInt(highlight.style.top) - 30).toString() + "px";
  88. legend.style.bottom = (parseInt(highlight.style.top) - 15).toString() + "px";
  89. imgCounter.innerHTML = (index + 1) + " / " + window.index;
  90. legend.innerHTML = image.alt
  91. // Position the close button just right:
  92. closebutton.style.top = (parseInt(highlight.style.top) - 14) + "px";
  93. closebutton.style.left = (parseInt(highlight.style.left) + dimensions.width - 8) + "px";
  94. function closeLightbox() {
  95. try {
  96. document.body.removeChild(highlight);
  97. document.body.removeChild(shade);
  98. document.body.removeChild(closebutton);
  99. document.body.removeChild(imgCounter);
  100. document.body.removeChild(legend);
  101. return true;
  102. }
  103. catch(NotFoundError) {
  104. return false;
  105. }
  106. }
  107. function nextImage() {
  108. if (window.index == 0) return;
  109. if (index < window.index -1) { // If this is false, we are on the last guy.
  110. closeLightbox();
  111. showHighlights(slbPics[index + 1]);
  112. }
  113. else {
  114. closeLightbox();
  115. showHighlights(slbPics[0]);
  116. }
  117. }
  118. function prevImage() {
  119. if (window.index == 0) return;
  120. if (index > 0) { // As long as not the first guy...
  121. closeLightbox();
  122. showHighlights(slbPics[index - 1]);
  123. }
  124. else {
  125. closeLightbox();
  126. showHighlights(slbPics[window.index - 1]);
  127. }
  128. }
  129. /*
  130. * Keyboard shortcuts defined to improve navigation:
  131. *
  132. * -ESC: quits simplelightbox, goes back to normal page view.
  133. * -SPACE: goes to the next image in the list.
  134. * -RIGHT ARROW: same as space.
  135. * -LEFT ARROW: inverse of space - goes to the previous image.
  136. */
  137. if (window.addEventListener) {
  138. window.addEventListener("keydown", function(e) {
  139. if (e.keyCode == 27) {
  140. closeLightbox();
  141. }
  142. else if (e.keyCode == 32 || e.keyCode == 39) {
  143. if (closeLightbox()) {
  144. nextImage();
  145. }
  146. }
  147. else if (e.keyCode == 37) {
  148. if (closeLightbox()) {
  149. prevImage();
  150. }
  151. }
  152. });
  153. } else if (window.attachEvent) {
  154. window.attachEvent("onkeydown", function(e) {
  155. if (e.keyCode == 27) {
  156. closeLightbox();
  157. }
  158. else if (e.keyCode == 32) {
  159. if (closeLightbox()) {
  160. nextImage();
  161. }
  162. }
  163. else if (e.keyCode == 37 || e.keyCode == 39) {
  164. if (closeLightbox()) {
  165. prevImage();
  166. }
  167. }
  168. });
  169. }
  170. highlight.onclick = function() {
  171. nextImage();
  172. }
  173. shade.onclick = closebutton.onclick = function() {
  174. closeLightbox();
  175. }
  176. }
  177. // the kickstarter!
  178. (function slbInitialize() {
  179. window.slbPics = document.querySelectorAll('img.slbPhoto');
  180. if (window.slbPics.length === 0) {
  181. window.slbPics = document.querySelectorAll("img");
  182. }
  183. for (var i = 0; i < slbPics.length; i++) {
  184. var a = new Picture(slbPics[i]);
  185. }
  186. } ());