ccc094b1eae11078038ddb15e5fd3b4df6442c7c.svn-base 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /***
  2. @title:
  3. Image Zoom
  4. @version:
  5. 2.0
  6. @author:
  7. Andreas Lagerkvist
  8. @date:
  9. 2008-08-31
  10. @url:
  11. http://andreaslagerkvist.com/jquery/image-zoom/
  12. @license:
  13. http://creativecommons.org/licenses/by/3.0/
  14. @copyright:
  15. 2008 Andreas Lagerkvist (andreaslagerkvist.com)
  16. @requires:
  17. jquery, jquery.imageZoom.css, jquery.imageZoom.png
  18. @does:
  19. This plug-in makes links pointing to images open in the "Image Zoom". Clicking a link will zoom out the clicked image to its target-image. Click anywhere on the image or the close-button to zoom the image back in. Only ~3k minified.
  20. @howto:
  21. jQuery(document.body).imageZoom(); Would make every link pointing to an image in the document open in the zoom.
  22. @exampleHTML:
  23. <ul>
  24. <li><a href="http://exscale.se/__files/3d/bloodcells.jpg">Bloodcells</a></li>
  25. <li><a href="http://exscale.se/__files/3d/x-wing.jpg">X-Wing</a></li>
  26. <li><a href="http://exscale.se/__files/3d/weve-moved.jpg">We've moved</a></li>
  27. </ul>
  28. <ul>
  29. <li><a href="http://exscale.se/__files/3d/lamp-and-mates/lamp-and-mates-01.jpg"><img src="http://exscale.se/__files/3d/lamp-and-mates/lamp-and-mates-01_small.jpg" alt="Lamp and Mates" /></a></li>
  30. <li><a href="http://exscale.se/__files/3d/stugan-winter.jpg"><img src="http://exscale.se/__files/3d/stugan-winter_small.jpg" alt="The Cottage - Winter time" /></a></li>
  31. <li><a href="http://exscale.se/__files/3d/ps2.jpg"><img src="http://exscale.se/__files/3d/ps2_small.jpg" alt="PS2" /></a></li>
  32. </ul>
  33. @exampleJS:
  34. // I don't run it because my site already uses imgZoom
  35. // jQuery(document.body).imageZoom();
  36. ***/
  37. jQuery.fn.imageZoom = function (conf) {
  38. // Some config. If you set dontFadeIn: 0 and hideClicked: 0 imgzoom will act exactly like fancyzoom
  39. var config = jQuery.extend({
  40. speed: 200, // Animation-speed of zoom
  41. dontFadeIn: 1, // 1 = Do not fade in, 0 = Do fade in
  42. hideClicked: 0, // Whether to hide the image that was clicked to bring up the imgzoom
  43. imageMargin: 15, // Margin from image-edge to window-edge if image is larger than screen
  44. className: 'jquery-image-zoom',
  45. loading: 'Loading...'
  46. }, conf);
  47. config.doubleSpeed = config.speed / 4; // Used for fading in the close-button
  48. return this.click(function(e) {
  49. // Make sure the target-element is a link (or an element inside a link)
  50. var clickedElement = jQuery(e.target); // The element that was actually clicked
  51. var clickedLink = clickedElement.is('a') ? clickedElement : clickedElement.parents('a'); // If it's not an a, check if any of its parents is
  52. clickedLink = (clickedLink && clickedLink.is('a') && clickedLink.attr('href').search(/(.*)\.(jpg|jpeg|gif|png|bmp|tif|tiff)$/gi) != -1) ? clickedLink : false; // If it was an a or child of an a, make sure it points to an image
  53. var clickedImg = (clickedLink && clickedLink.find('img').length) ? clickedLink.find('img') : false; // See if the clicked link contains and image
  54. // Only continue if a link pointing to an image was clicked
  55. if (clickedLink) {
  56. // These functions are used when the imaeg starts and stops loading (displays either 'loading..' or fades out the clicked img slightly)
  57. clickedLink.oldText = clickedLink.text();
  58. clickedLink.setLoadingImg = function () {
  59. if (clickedImg) {
  60. clickedImg.css({opacity: '0.5'});
  61. }
  62. else {
  63. clickedLink.text(config.loading);
  64. }
  65. };
  66. clickedLink.setNotLoadingImg = function () {
  67. if (clickedImg) {
  68. clickedImg.css({opacity: '1'});
  69. }
  70. else {
  71. clickedLink.text(clickedLink.oldText);
  72. }
  73. };
  74. // The URI to the image we are going to display
  75. var displayImgSrc = clickedLink.attr('href');
  76. // If an imgzoom wiv this image is already open dont do nathin
  77. if (jQuery('div.' + config.className + ' img[src="' + displayImgSrc + '"]').length) {
  78. return false;
  79. }
  80. // This function is run once the displayImgSrc-img has loaded (below)
  81. var preloadOnload = function () {
  82. // The clicked-link is faded out during loading, fade it back in
  83. clickedLink.setNotLoadingImg();
  84. // Now set some vars we need
  85. var dimElement = clickedImg ? clickedImg : clickedLink; // The element used to retrieve dimensions of imgzoom before zoom (either clicked link or img inside)
  86. var hideClicked = clickedImg ? config.hideClicked : 0; // Whether to hide clicked link (set in config but always true for non-image-links)
  87. var offset = dimElement.offset(); // Offset of clicked link (or image inside)
  88. var imgzoomBefore = { // The dimensions of the imgzoom _before_ it is zoomed out
  89. width: dimElement.outerWidth(),
  90. height: dimElement.outerHeight(),
  91. left: offset.left,
  92. top: offset.top/*,
  93. opacity: config.dontFadeIn*/
  94. };
  95. var imgzoom = jQuery('<div><img src="' + displayImgSrc + '" alt="" /></div>').css('position', 'absolute').appendTo(document.body); // We don't want any class-name or any other contents part from the image when we calculate the new dimensions of the imgzoom
  96. var imgzoomAfter = { // The dimensions of the imgzoom _after_ it is zoomed out
  97. width: imgzoom.outerWidth(),
  98. height: imgzoom.outerHeight()/*,
  99. opacity: 1*/
  100. };
  101. var windowDim = {
  102. width: jQuery(window).width(),
  103. height: jQuery(window).height()
  104. };
  105. // Make sure imgzoom isn't wider than screen
  106. if (imgzoomAfter.width > (windowDim.width - config.imageMargin * 2)) {
  107. var nWidth = windowDim.width - config.imageMargin * 2;
  108. imgzoomAfter.height = (nWidth / imgzoomAfter.width) * imgzoomAfter.height;
  109. imgzoomAfter.width = nWidth;
  110. }
  111. // Now make sure it isn't taller
  112. if (imgzoomAfter.height > (windowDim.height - config.imageMargin * 2)) {
  113. var nHeight = windowDim.height - config.imageMargin * 2;
  114. imgzoomAfter.width = (nHeight / imgzoomAfter.height) * imgzoomAfter.width;
  115. imgzoomAfter.height = nHeight;
  116. }
  117. // Center imgzoom
  118. imgzoomAfter.left = (windowDim.width - imgzoomAfter.width) / 2 + jQuery(window).scrollLeft();
  119. imgzoomAfter.top = (windowDim.height - imgzoomAfter.height) / 2 + jQuery(window).scrollTop();
  120. var closeButton = jQuery('<a href="#">Close</a>').appendTo(imgzoom).hide(); // The button that closes the imgzoom (we're adding this after the calculation of the dimensions)
  121. // Hide the clicked link if set so in config
  122. if (hideClicked) {
  123. clickedLink.css('visibility', 'hidden');
  124. }
  125. // Now animate the imgzoom from its small size to its large size, and then fade in the close-button
  126. imgzoom.addClass(config.className).css(imgzoomBefore).animate(imgzoomAfter, config.speed, function () {
  127. closeButton.fadeIn(config.doubleSpeed);
  128. });
  129. // This function closes the imgzoom
  130. var hideImgzoom = function () {
  131. closeButton.fadeOut(config.doubleSpeed, function () {
  132. imgzoom.animate(imgzoomBefore, config.speed, function () {
  133. clickedLink.css('visibility', 'visible');
  134. imgzoom.remove();
  135. });
  136. });
  137. return false;
  138. };
  139. // Close imgzoom when you click the closeButton or the imgzoom
  140. imgzoom.click(hideImgzoom);
  141. closeButton.click(hideImgzoom);
  142. };
  143. // Preload image
  144. var preload = new Image();
  145. preload.src = displayImgSrc;
  146. if (preload.complete) {
  147. preloadOnload();
  148. }
  149. else {
  150. clickedLink.setLoadingImg();
  151. preload.onload = preloadOnload;
  152. }
  153. // Finally return false from the click so the browser doesn't actually follow the link...
  154. return false;
  155. }
  156. });
  157. };
  158. // Close image zooms when user hits esc
  159. $(document).keydown(function (e) {
  160. if (e.keyCode == 27) {
  161. $('div.jquery-image-zoom a').click();
  162. }
  163. });