jquery.lightbox.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /**
  2. * jQuery lightBox plugin
  3. * This jQuery plugin was inspired and based on Lightbox 2 by Lokesh Dhakar (http://www.huddletogether.com/projects/lightbox2/)
  4. * and adapted to me for use like a plugin from jQuery.
  5. * @name jquery-lightbox-0.5.js
  6. * @author Leandro Vieira Pinho-http://leandrovieira.com
  7. * @version 0.5
  8. * @date April 11, 2008
  9. * @category jQuery plugin
  10. * @copyright (c) 2008 Leandro Vieira Pinho (leandrovieira.com)
  11. * @license CCAttribution-ShareAlike 2.5 Brazil-http://creativecommons.org/licenses/by-sa/2.5/br/deed.en_US
  12. * @example Visit http://leandrovieira.com/projects/jquery/lightbox/ for more informations about this jQuery plugin
  13. */
  14. // Offering a Custom Alias suport-More info: http://docs.jquery.com/Plugins/Authoring#Custom_Alias
  15. (function($) {
  16. /**
  17. * $ is an alias to jQuery object
  18. *
  19. */
  20. $.fn.lightBox = function(settings) {
  21. // Settings to configure the jQuery lightBox plugin how you like
  22. settings = jQuery.extend({
  23. // Configuration related to overlay
  24. overlayBgColor: '#000', // (string) Background color to overlay; inform a hexadecimal value like: #RRGGBB. Where RR, GG, and BB are the hexadecimal values for the red, green, and blue values of the color.
  25. overlayOpacity: 0.8, // (integer) Opacity value to overlay; inform: 0.X. Where X are number from 0 to 9
  26. // Configuration related to navigation
  27. fixedNavigation: false, // (boolean) Boolean that informs if the navigation (next and prev button) will be fixed or not in the interface.
  28. // Configuration related to images
  29. imageLoading: current_domain+'/template/default/images/lightbox/loading.gif', // (string) Path and the name of the loading icon
  30. imageBtnPrev: current_domain+'/template/default/images/lightbox/prev.gif', // (string) Path and the name of the prev button image
  31. imageBtnNext: current_domain+'/template/default/images/lightbox/next.gif', // (string) Path and the name of the next button image
  32. imageBtnClose: current_domain+'/template/default/images/lightbox/close.gif', // (string) Path and the name of the close btn
  33. imageBlank: current_domain+'/template/default/images/lightbox/blank.gif', // (string) Path and the name of a blank image (one pixel)
  34. // Configuration related to container image box
  35. containerBorderSize: 10, // (integer) If you adjust the padding in the CSS for the container, #lightbox-container-image-box, you will need to update this value
  36. containerResizeSpeed: 400, // (integer) Specify the resize duration of container image. These number are miliseconds. 400 is default.
  37. // Configuration related to texts in caption. For example: Image 2 of 8. You can alter either "Image" and "of" texts.
  38. txtImage: '图', // (string) Specify text "Image"
  39. txtOf: '×', // (string) Specify text "of"
  40. // Configuration related to keyboard navigation
  41. keyToClose: 'c', // (string) (c = close) Letter to close the jQuery lightBox interface. Beyond this letter, the letter X and the SCAPE key is used to.
  42. keyToPrev: 'p', // (string) (p = previous) Letter to show the previous image
  43. keyToNext: 'n', // (string) (n = next) Letter to show the next image.
  44. // Don磘 alter these variables in any way
  45. imageArray: [],
  46. activeImage: 0
  47. },settings);
  48. // Caching the jQuery object with all elements matched
  49. var jQueryMatchedObj = this; // This, in this context, refer to jQuery object
  50. /**
  51. * Initializing the plugin calling the start function
  52. *
  53. * @return boolean false
  54. */
  55. function _initialize() {
  56. _start(this,jQueryMatchedObj); // This, in this context, refer to object (link) which the user have clicked
  57. return false; // Avoid the browser following the link
  58. }
  59. /**
  60. * Start the jQuery lightBox plugin
  61. *
  62. * @param object objClicked The object (link) whick the user have clicked
  63. * @param object jQueryMatchedObj The jQuery object with all elements matched
  64. */
  65. function _start(objClicked,jQueryMatchedObj) {
  66. // Hime some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
  67. $('embed, object, select').css({ 'visibility' : 'hidden' });
  68. // Call the function to create the markup structure; style some elements; assign events in some elements.
  69. _set_interface();
  70. // Unset total images in imageArray
  71. settings.imageArray.length = 0;
  72. // Unset image active information
  73. settings.activeImage = 0;
  74. // We have an image set? Or just an image? Let磗 see it.
  75. if ( jQueryMatchedObj.length == 1 ) {
  76. settings.imageArray.push(new Array(objClicked.getAttribute('href'),objClicked.getAttribute('title')));
  77. } else {
  78. // Add an Array (as many as we have), with href and title atributes, inside the Array that storage the images references
  79. for ( var i = 0; i < jQueryMatchedObj.length; i++ ) {
  80. settings.imageArray.push(new Array(jQueryMatchedObj[i].getAttribute('href'),jQueryMatchedObj[i].getAttribute('title')));
  81. }
  82. }
  83. while ( settings.imageArray[settings.activeImage][0] != objClicked.getAttribute('href') ) {
  84. settings.activeImage++;
  85. }
  86. // Call the function that prepares image exibition
  87. _set_image_to_view();
  88. }
  89. /**
  90. * Create the jQuery lightBox plugin interface
  91. *
  92. * The HTML markup will be like that:
  93. <div id="jquery-overlay"></div>
  94. <div id="jquery-lightbox">
  95. <div id="lightbox-container-image-box">
  96. <div id="lightbox-container-image">
  97. <img src="../fotos/XX.jpg" id="lightbox-image">
  98. <div id="lightbox-nav">
  99. <a href="#" id="lightbox-nav-btnPrev"></a>
  100. <a href="#" id="lightbox-nav-btnNext"></a>
  101. </div>
  102. <div id="lightbox-loading">
  103. <a href="#" id="lightbox-loading-link">
  104. <img src="../images/lightbox-ico-loading.gif">
  105. </a>
  106. </div>
  107. </div>
  108. </div>
  109. <div id="lightbox-container-image-data-box">
  110. <div id="lightbox-container-image-data">
  111. <div id="lightbox-image-details">
  112. <span id="lightbox-image-details-caption"></span>
  113. <span id="lightbox-image-details-currentNumber"></span>
  114. </div>
  115. <div id="lightbox-secNav">
  116. <a href="#" id="lightbox-secNav-btnClose">
  117. <img src="../images/lightbox-btn-close.gif">
  118. </a>
  119. </div>
  120. </div>
  121. </div>
  122. </div>
  123. *
  124. */
  125. function _set_interface() {
  126. // Apply the HTML markup into body tag
  127. $('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="lightbox-container-image-box"><div id="lightbox-container-image"><img id="lightbox-image"><div style="" id="lightbox-nav"><a href="#" id="lightbox-nav-btnPrev"></a><a href="#" id="lightbox-nav-btnNext"></a></div><div id="lightbox-loading"><a href="#" id="lightbox-loading-link"><img src="' + settings.imageLoading + '"></a></div></div></div><div id="lightbox-container-image-data-box"><div id="lightbox-container-image-data"><div id="lightbox-image-details"><span id="lightbox-image-details-caption"></span><span id="lightbox-image-details-currentNumber" style="font-size:14px;"></span></div><div id="lightbox-secNav"><a href="#" id="lightbox-secNav-btnClose"><img src="' + settings.imageBtnClose + '"></a></div></div></div></div>');
  128. // Get page sizes
  129. var arrPageSizes = ___getPageSize();
  130. // Style overlay and show it
  131. $('#jquery-overlay').css({
  132. backgroundColor: settings.overlayBgColor,
  133. opacity: settings.overlayOpacity,
  134. width: arrPageSizes[0],
  135. height: arrPageSizes[1]
  136. }).fadeIn();
  137. // Get page scroll
  138. var arrPageScroll = ___getPageScroll();
  139. // Calculate top and left offset for the jquery-lightbox div object and show it
  140. $('#jquery-lightbox').css({
  141. top: arrPageScroll[1] + (arrPageSizes[3] / 10),
  142. left: arrPageScroll[0]
  143. }).show();
  144. // Assigning click events in elements to close overlay
  145. $('#jquery-overlay,#jquery-lightbox').click(function() {
  146. _finish();
  147. });
  148. // Assign the _finish function to lightbox-loading-link and lightbox-secNav-btnClose objects
  149. $('#lightbox-loading-link,#lightbox-secNav-btnClose').click(function() {
  150. _finish();
  151. return false;
  152. });
  153. // If window was resized, calculate the new overlay dimensions
  154. $(window).resize(function() {
  155. // Get page sizes
  156. var arrPageSizes = ___getPageSize();
  157. // Style overlay and show it
  158. $('#jquery-overlay').css({
  159. width: arrPageSizes[0],
  160. height: arrPageSizes[1]
  161. });
  162. // Get page scroll
  163. var arrPageScroll = ___getPageScroll();
  164. // Calculate top and left offset for the jquery-lightbox div object and show it
  165. $('#jquery-lightbox').css({
  166. top: arrPageScroll[1] + (arrPageSizes[3] / 10),
  167. left: arrPageScroll[0]
  168. });
  169. });
  170. }
  171. /**
  172. * Prepares image exibition; doing a image磗 preloader to calculate it磗 size
  173. *
  174. */
  175. function _set_image_to_view() { // show the loading
  176. // Show the loading
  177. $('#lightbox-loading').show();
  178. if ( settings.fixedNavigation ) {
  179. $('#lightbox-image,#lightbox-container-image-data-box,#lightbox-image-details-currentNumber').hide();
  180. } else {
  181. // Hide some elements
  182. $('#lightbox-image,#lightbox-nav,#lightbox-nav-btnPrev,#lightbox-nav-btnNext,#lightbox-container-image-data-box,#lightbox-image-details-currentNumber').hide();
  183. }
  184. // Image preload process
  185. var objImagePreloader = new Image();
  186. objImagePreloader.onload = function() {
  187. $('#lightbox-image').attr('src',settings.imageArray[settings.activeImage][0]);
  188. // Perfomance an effect in the image container resizing it
  189. _resize_container_image_box(objImagePreloader.width,objImagePreloader.height);
  190. // clear onLoad, IE behaves irratically with animated gifs otherwise
  191. objImagePreloader.onload=function(){};
  192. };
  193. objImagePreloader.src = settings.imageArray[settings.activeImage][0];
  194. };
  195. /**
  196. * Perfomance an effect in the image container resizing it
  197. *
  198. * @param integer intImageWidth The image磗 width that will be showed
  199. * @param integer intImageHeight The image磗 height that will be showed
  200. */
  201. function _resize_container_image_box(intImageWidth,intImageHeight) {
  202. // Get current width and height
  203. var intCurrentWidth = $('#lightbox-container-image-box').width();
  204. var intCurrentHeight = $('#lightbox-container-image-box').height();
  205. // Get the width and height of the selected image plus the padding
  206. var intWidth = (intImageWidth + (settings.containerBorderSize * 2)); // Plus the image磗 width and the left and right padding value
  207. var intHeight = (intImageHeight + (settings.containerBorderSize * 2)); // Plus the image磗 height and the left and right padding value
  208. // Diferences
  209. var intDiffW = intCurrentWidth-intWidth;
  210. var intDiffH = intCurrentHeight-intHeight;
  211. // Perfomance the effect
  212. $('#lightbox-container-image-box').animate({ width: intWidth, height: intHeight },settings.containerResizeSpeed,function() { _show_image(); });
  213. if ( ( intDiffW == 0 ) && ( intDiffH == 0 ) ) {
  214. if ( $.browser.msie ) {
  215. ___pause(250);
  216. } else {
  217. ___pause(100);
  218. }
  219. }
  220. $('#lightbox-container-image-data-box').css({ width: intImageWidth });
  221. $('#lightbox-nav-btnPrev,#lightbox-nav-btnNext').css({ height: intImageHeight + (settings.containerBorderSize * 2) });
  222. };
  223. /**
  224. * Show the prepared image
  225. *
  226. */
  227. function _show_image() {
  228. $('#lightbox-loading').hide();
  229. $('#lightbox-image').fadeIn(function() {
  230. _show_image_data();
  231. _set_navigation();
  232. });
  233. _preload_neighbor_images();
  234. };
  235. /**
  236. * Show the image information
  237. *
  238. */
  239. function _show_image_data() {
  240. $('#lightbox-container-image-data-box').slideDown('fast');
  241. $('#lightbox-image-details-caption').hide();
  242. if ( settings.imageArray[settings.activeImage][1] ) {
  243. $('#lightbox-image-details-caption').html(settings.imageArray[settings.activeImage][1]).show();
  244. }
  245. // If we have a image set, display 'Image X of X'
  246. if ( settings.imageArray.length > 1 ) {
  247. $('#lightbox-image-details-currentNumber').html(settings.txtImage + ' ' + ( settings.activeImage + 1 ) + ' ' + settings.txtOf + ' ' + settings.imageArray.length).show();
  248. }
  249. }
  250. /**
  251. * Display the button navigations
  252. *
  253. */
  254. function _set_navigation() {
  255. $('#lightbox-nav').show();
  256. // Instead to define this configuration in CSS file, we define here. And it磗 need to IE. Just.
  257. $('#lightbox-nav-btnPrev,#lightbox-nav-btnNext').css({ 'background' : 'transparent url(' + settings.imageBlank + ') no-repeat' });
  258. // Show the prev button, if not the first image in set
  259. if ( settings.activeImage != 0 ) {
  260. if ( settings.fixedNavigation ) {
  261. $('#lightbox-nav-btnPrev').css({ 'background' : 'url(' + settings.imageBtnPrev + ') left 15% no-repeat' })
  262. .unbind()
  263. .bind('click',function() {
  264. settings.activeImage = settings.activeImage-1;
  265. _set_image_to_view();
  266. return false;
  267. });
  268. } else {
  269. // Show the images button for Next buttons
  270. $('#lightbox-nav-btnPrev').unbind().hover(function() {
  271. $(this).css({ 'background' : 'url(' + settings.imageBtnPrev + ') left 15% no-repeat' });
  272. },function() {
  273. $(this).css({ 'background' : 'transparent url(' + settings.imageBlank + ') no-repeat' });
  274. }).show().bind('click',function() {
  275. settings.activeImage = settings.activeImage-1;
  276. _set_image_to_view();
  277. return false;
  278. });
  279. }
  280. }
  281. // Show the next button, if not the last image in set
  282. if ( settings.activeImage != ( settings.imageArray.length -1 ) ) {
  283. if ( settings.fixedNavigation ) {
  284. $('#lightbox-nav-btnNext').css({ 'background' : 'url(' + settings.imageBtnNext + ') right 15% no-repeat' })
  285. .unbind()
  286. .bind('click',function() {
  287. settings.activeImage = settings.activeImage + 1;
  288. _set_image_to_view();
  289. return false;
  290. });
  291. } else {
  292. // Show the images button for Next buttons
  293. $('#lightbox-nav-btnNext').unbind().hover(function() {
  294. $(this).css({ 'background' : 'url(' + settings.imageBtnNext + ') right 15% no-repeat' });
  295. },function() {
  296. $(this).css({ 'background' : 'transparent url(' + settings.imageBlank + ') no-repeat' });
  297. }).show().bind('click',function() {
  298. settings.activeImage = settings.activeImage + 1;
  299. _set_image_to_view();
  300. return false;
  301. });
  302. }
  303. }
  304. // Enable keyboard navigation
  305. _enable_keyboard_navigation();
  306. }
  307. /**
  308. * Enable a support to keyboard navigation
  309. *
  310. */
  311. function _enable_keyboard_navigation() {
  312. $(document).keydown(function(objEvent) {
  313. _keyboard_action(objEvent);
  314. });
  315. }
  316. /**
  317. * Disable the support to keyboard navigation
  318. *
  319. */
  320. function _disable_keyboard_navigation() {
  321. $(document).unbind();
  322. }
  323. /**
  324. * Perform the keyboard actions
  325. *
  326. */
  327. function _keyboard_action(objEvent) {
  328. // To ie
  329. if ( objEvent == null ) {
  330. keycode = event.keyCode;
  331. escapeKey = 27;
  332. // To Mozilla
  333. } else {
  334. keycode = objEvent.keyCode;
  335. escapeKey = objEvent.DOM_VK_ESCAPE;
  336. }
  337. // Get the key in lower case form
  338. key = String.fromCharCode(keycode).toLowerCase();
  339. // Verify the keys to close the ligthBox
  340. if ( ( key == settings.keyToClose ) || ( key == 'x' ) || ( keycode == escapeKey ) ) {
  341. _finish();
  342. }
  343. // Verify the key to show the previous image
  344. if ( ( key == settings.keyToPrev ) || ( keycode == 37 ) ) {
  345. // If we磖e not showing the first image, call the previous
  346. if ( settings.activeImage != 0 ) {
  347. settings.activeImage = settings.activeImage-1;
  348. _set_image_to_view();
  349. _disable_keyboard_navigation();
  350. }
  351. }
  352. // Verify the key to show the next image
  353. if ( ( key == settings.keyToNext ) || ( keycode == 39 ) ) {
  354. // If we磖e not showing the last image, call the next
  355. if ( settings.activeImage != ( settings.imageArray.length-1 ) ) {
  356. settings.activeImage = settings.activeImage + 1;
  357. _set_image_to_view();
  358. _disable_keyboard_navigation();
  359. }
  360. }
  361. }
  362. /**
  363. * Preload prev and next images being showed
  364. *
  365. */
  366. function _preload_neighbor_images() {
  367. if ( (settings.imageArray.length -1) > settings.activeImage ) {
  368. objNext = new Image();
  369. objNext.src = settings.imageArray[settings.activeImage + 1][0];
  370. }
  371. if ( settings.activeImage > 0 ) {
  372. objPrev = new Image();
  373. objPrev.src = settings.imageArray[settings.activeImage -1][0];
  374. }
  375. }
  376. /**
  377. * Remove jQuery lightBox plugin HTML markup
  378. *
  379. */
  380. function _finish() {
  381. $('#jquery-lightbox').remove();
  382. $('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
  383. // Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
  384. $('embed, object, select').css({ 'visibility' : 'visible' });
  385. }
  386. /**
  387. / THIRD FUNCTION
  388. * getPageSize() by quirksmode.com
  389. *
  390. * @return Array Return an array with page width, height and window width, height
  391. */
  392. function ___getPageSize() {
  393. var xScroll, yScroll;
  394. if (window.innerHeight && window.scrollMaxY) {
  395. xScroll = window.innerWidth + window.scrollMaxX;
  396. yScroll = window.innerHeight + window.scrollMaxY;
  397. } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
  398. xScroll = document.body.scrollWidth;
  399. yScroll = document.body.scrollHeight;
  400. } else { // Explorer Macwould also work in Explorer 6 Strict, Mozilla and Safari
  401. xScroll = document.body.offsetWidth;
  402. yScroll = document.body.offsetHeight;
  403. }
  404. var windowWidth, windowHeight;
  405. if (self.innerHeight) { // all except Explorer
  406. if(document.documentElement.clientWidth){
  407. windowWidth = document.documentElement.clientWidth;
  408. } else {
  409. windowWidth = self.innerWidth;
  410. }
  411. windowHeight = self.innerHeight;
  412. } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
  413. windowWidth = document.documentElement.clientWidth;
  414. windowHeight = document.documentElement.clientHeight;
  415. } else if (document.body) { // other Explorers
  416. windowWidth = document.body.clientWidth;
  417. windowHeight = document.body.clientHeight;
  418. }
  419. // for small pages with total height less then height of the viewport
  420. if(yScroll < windowHeight){
  421. pageHeight = windowHeight;
  422. } else {
  423. pageHeight = yScroll;
  424. }
  425. // for small pages with total width less then width of the viewport
  426. if(xScroll < windowWidth){
  427. pageWidth = xScroll;
  428. } else {
  429. pageWidth = windowWidth;
  430. }
  431. arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
  432. return arrayPageSize;
  433. };
  434. /**
  435. / THIRD FUNCTION
  436. * getPageScroll() by quirksmode.com
  437. *
  438. * @return Array Return an array with x,y page scroll values.
  439. */
  440. function ___getPageScroll() {
  441. var xScroll, yScroll;
  442. if (self.pageYOffset) {
  443. yScroll = self.pageYOffset;
  444. xScroll = self.pageXOffset;
  445. } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
  446. yScroll = document.documentElement.scrollTop;
  447. xScroll = document.documentElement.scrollLeft;
  448. } else if (document.body) {// all other Explorers
  449. yScroll = document.body.scrollTop;
  450. xScroll = document.body.scrollLeft;
  451. }
  452. arrayPageScroll = new Array(xScroll,yScroll);
  453. return arrayPageScroll;
  454. };
  455. /**
  456. * Stop the code execution from a escified time in milisecond
  457. *
  458. */
  459. function ___pause(ms) {
  460. var date = new Date();
  461. curDate = null;
  462. do { var curDate = new Date(); }
  463. while ( curDate-date < ms);
  464. };
  465. // Return the jQuery object for chaining. The unbind method is used to avoid click conflict when the plugin is called more than once
  466. return this.unbind('click').click(_initialize);
  467. };
  468. })(jQuery); // Call and execute the function immediately passing the jQuery object