gallery.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. $(function() {
  2. // ======================= imagesLoaded Plugin ===============================
  3. // https://github.com/desandro/imagesloaded
  4. // $('#my-container').imagesLoaded(myFunction)
  5. // execute a callback when all images have loaded.
  6. // needed because .load() doesn't work on cached images
  7. // callback function gets image collection as argument
  8. // this is the container
  9. // original: mit license. paul irish. 2010.
  10. // contributors: Oren Solomianik, David DeSandro, Yiannis Chatzikonstantinou
  11. $.fn.imagesLoaded = function( callback ) {
  12. var $images = this.find('img'),
  13. len = $images.length,
  14. _this = this,
  15. blank = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';
  16. function triggerCallback() {
  17. callback.call( _this, $images );
  18. }
  19. function imgLoaded() {
  20. if ( --len <= 0 && this.src !== blank ){
  21. setTimeout( triggerCallback );
  22. $images.off( 'load error', imgLoaded );
  23. }
  24. }
  25. if ( !len ) {
  26. triggerCallback();
  27. }
  28. $images.on( 'load error', imgLoaded ).each( function() {
  29. // cached images don't fire load sometimes, so we reset src.
  30. if (this.complete || this.complete === undefined){
  31. var src = this.src;
  32. // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
  33. // data uri bypasses webkit log warning (thx doug jones)
  34. this.src = blank;
  35. this.src = src;
  36. }
  37. });
  38. return this;
  39. };
  40. // gallery container
  41. var $rgGallery = $('#rg-gallery'),
  42. // carousel container
  43. $esCarousel = $rgGallery.find('div.es-carousel-wrapper'),
  44. // the carousel items
  45. $items = $esCarousel.find('ul > li'),
  46. // total number of items
  47. itemsCount = $items.length;
  48. Gallery = (function() {
  49. // index of the current item
  50. var current = 0,
  51. // mode : carousel || fullview
  52. mode = 'carousel',
  53. // control if one image is being loaded
  54. anim = false,
  55. init = function() {
  56. // (not necessary) preloading the images here...
  57. $items.add('<img src="images/ajax-loader.gif"/><img src="images/black.png"/>').imagesLoaded( function() {
  58. // add options
  59. _addViewModes();
  60. // add large image wrapper
  61. _addImageWrapper();
  62. // show first image
  63. _showImage( $items.eq( current ) );
  64. });
  65. // initialize the carousel
  66. if( mode === 'carousel' )
  67. _initCarousel();
  68. },
  69. _initCarousel = function() {
  70. // we are using the elastislide plugin:
  71. // http://tympanus.net/codrops/2011/09/12/elastislide-responsive-carousel/
  72. $esCarousel.show().elastislide({
  73. imageW : 65,
  74. onClick : function( $item ) {
  75. if( anim ) return false;
  76. anim = true;
  77. // on click show image
  78. _showImage($item);
  79. // change current
  80. current = $item.index();
  81. }
  82. });
  83. // set elastislide's current to current
  84. $esCarousel.elastislide( 'setCurrent', current );
  85. },
  86. _addViewModes = function() {
  87. // top right buttons: hide / show carousel
  88. var $viewfull = $('<a href="#" class="rg-view-full"></a>'),
  89. $viewthumbs = $('<a href="#" class="rg-view-thumbs rg-view-selected"></a>');
  90. $rgGallery.prepend( $('<div class="rg-view"/>').append( $viewfull ).append( $viewthumbs ) );
  91. $viewfull.on('click.rgGallery', function( event ) {
  92. if( mode === 'carousel' )
  93. $esCarousel.elastislide( 'destroy' );
  94. $esCarousel.hide();
  95. $viewfull.addClass('rg-view-selected');
  96. $viewthumbs.removeClass('rg-view-selected');
  97. mode = 'fullview';
  98. return false;
  99. });
  100. $viewthumbs.on('click.rgGallery', function( event ) {
  101. _initCarousel();
  102. $viewthumbs.addClass('rg-view-selected');
  103. $viewfull.removeClass('rg-view-selected');
  104. mode = 'carousel';
  105. return false;
  106. });
  107. if( mode === 'fullview' )
  108. $viewfull.trigger('click');
  109. },
  110. _addImageWrapper= function() {
  111. // adds the structure for the large image and the navigation buttons (if total items > 1)
  112. // also initializes the navigation events
  113. $('#img-wrapper-tmpl').tmpl( {itemsCount : itemsCount} ).appendTo( $rgGallery );
  114. if( itemsCount > 1 ) {
  115. // addNavigation
  116. var $navPrev = $rgGallery.find('a.rg-image-nav-prev'),
  117. $navNext = $rgGallery.find('a.rg-image-nav-next'),
  118. $imgWrapper = $rgGallery.find('div.rg-image');
  119. $navPrev.on('click.rgGallery', function( event ) {
  120. _navigate( 'left' );
  121. return false;
  122. });
  123. $navNext.on('click.rgGallery', function( event ) {
  124. _navigate( 'right' );
  125. return false;
  126. });
  127. // add touchwipe events on the large image wrapper
  128. $imgWrapper.touchwipe({
  129. wipeLeft : function() {
  130. _navigate( 'right' );
  131. },
  132. wipeRight : function() {
  133. _navigate( 'left' );
  134. },
  135. preventDefaultEvents: false
  136. });
  137. $(document).on('keyup.rgGallery', function( event ) {
  138. if (event.keyCode == 39)
  139. _navigate( 'right' );
  140. else if (event.keyCode == 37)
  141. _navigate( 'left' );
  142. });
  143. }
  144. },
  145. _navigate = function( dir ) {
  146. // navigate through the large images
  147. if( anim ) return false;
  148. anim = true;
  149. if( dir === 'right' ) {
  150. if( current + 1 >= itemsCount )
  151. current = 0;
  152. else
  153. ++current;
  154. }
  155. else if( dir === 'left' ) {
  156. if( current - 1 < 0 )
  157. current = itemsCount - 1;
  158. else
  159. --current;
  160. }
  161. _showImage( $items.eq( current ) );
  162. },
  163. _showImage = function( $item ) {
  164. // shows the large image that is associated to the $item
  165. var $loader = $rgGallery.find('div.rg-loading').show();
  166. $items.removeClass('selected');
  167. $item.addClass('selected');
  168. var $thumb = $item.find('img'),
  169. largesrc = $thumb.data('large'),
  170. title = $thumb.data('description');
  171. $('<img/>').load( function() {
  172. $rgGallery.find('div.rg-image').empty().append('<img src="' + largesrc + '"/>');
  173. if( title )
  174. $rgGallery.find('div.rg-caption').show().children('p').empty().text( title );
  175. $loader.hide();
  176. if( mode === 'carousel' ) {
  177. $esCarousel.elastislide( 'reload' );
  178. $esCarousel.elastislide( 'setCurrent', current );
  179. }
  180. anim = false;
  181. }).attr( 'src', largesrc );
  182. },
  183. addItems = function( $new ) {
  184. $esCarousel.find('ul').append($new);
  185. $items = $items.add( $($new) );
  186. itemsCount = $items.length;
  187. $esCarousel.elastislide( 'add', $new );
  188. };
  189. return {
  190. init : init,
  191. addItems : addItems
  192. };
  193. })();
  194. Gallery.init();
  195. /*
  196. Example to add more items to the gallery:
  197. var $new = $('<li><a href="#"><img src="images/thumbs/1.jpg" data-large="images/1.jpg" alt="image01" data-description="From off a hill whose concave womb reworded" /></a></li>');
  198. Gallery.addItems( $new );
  199. */
  200. });