jquery.sumogallery.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*!
  2. * jquery.sumogallery - v1.0.0
  3. * http://hemantnegi.github.io/jquery.sumogallery
  4. * created : 2015-01-21
  5. *
  6. * Copyright 2014 Hemant Negi
  7. * Email : hemant.frnz@gmail.com
  8. * Compressor http://refresh-sf.com/yui/
  9. */
  10. (function ($) {
  11. 'namespace sg';
  12. sgallery = {
  13. settings: {
  14. sattr: 'data-sgallery', // attrribute on image to be looked by plugin. by which we can set group name.
  15. showImgCount: true, //display image counts on bottom right
  16. showThumbs: false
  17. },
  18. //bind all generic events here.
  19. bindEvents: function () {
  20. var O = this;
  21. var S = O.settings;
  22. //add click to all the images having attribute data-sgallery
  23. $('img[' + S.sattr + ']').on('click.sg', function () {
  24. O.SG.reset();
  25. var _d = O.prepareData($(this));
  26. O.prepareHTML(_d);
  27. });
  28. },
  29. //accepts current clicked element.
  30. //returns a json object
  31. prepareData: function (c_img) {
  32. var O = this;
  33. var S = O.settings;
  34. gname = c_img.attr(S.sattr);
  35. var imgs = [];
  36. $('img[data-sgallery="' + gname + '"]').each(function (i, e) {
  37. e = $(e);
  38. var d = {};
  39. d['thumb'] = e.data('thumb') || false;
  40. d['full'] = e.data('full') || false;
  41. d['title'] = e.attr('title') || '';
  42. d['alt'] = e.attr('alt') || '';
  43. if (d['thumb'] && d['full']) imgs.push(d); // exclude if thumb or full is not set.
  44. else return;
  45. //collect the index of selected item.
  46. if (e.is(c_img)) O.SG.sindex = i;
  47. });
  48. return imgs;
  49. },
  50. //prepares all the required markup for the gallery.
  51. prepareHTML: function (imgs) {
  52. var O = this;
  53. O.SG.imgs = imgs;
  54. //dont do any thing if no proper data available.
  55. if (imgs.length == 0) {
  56. console.log('no images with appropriate data!!');
  57. return;
  58. }
  59. $('.SumoGallery').remove();
  60. O.SG._SG = $('<div class="SumoGallery" style="display:none;">');
  61. if (!O.settings.showThumbs)O.SG._SG.addClass('nothumbs');
  62. O.SG.title = $('<p>Sumo Gallery</p>');
  63. O.SG.close = $('<a>&times;</a>');
  64. O.SG.body = $('<div class="Sbody">');
  65. O.SG.footer = $('<div class="Sfooter">');
  66. O.SG.thumbs = $('<ul class="Sthumbnails">');
  67. O.SG.footer.append(O.SG.thumbs);
  68. O.SG.left = $('<a class="Sarrow l">&#10092;</a>');
  69. O.SG.right = $('<a class="Sarrow r">&#10093;</a>');
  70. O.SG.count = $('<p class="SimgCount"></p>');
  71. O.SG.arrow = $('<a title="Toggle thumbnails." class="toggle-thumbs">&#8226;&#8226;&#8226;</a>');
  72. O.SG._SG.append($('<div class="Sheader">').append(O.SG.title).append(O.SG.close))
  73. .append(O.SG.body).append(O.SG.footer).append(O.SG.left).append(O.SG.right);
  74. O.SG.footer.append(O.SG.arrow).append(O.SG.count);
  75. for (var i in imgs) {
  76. // preload images technique
  77. O.SG.body.append($('<a>').append(
  78. $('<img alt="' + imgs[i].full + '" title="' + imgs[i].title + '" style="display:none;" />').on('load', function () {
  79. $(this).fadeIn(300);
  80. }).attr('src', imgs[i].full)
  81. ));
  82. O.SG.thumbs.append('<li><a style="background-image:url(\'' + imgs[i].thumb + '\')"></a><span>'+ imgs[i].title +'</span></li>');
  83. }
  84. $('body').append(O.SG._SG);
  85. O.SG.init();
  86. O.SG._SG.fadeIn(300);
  87. },
  88. // actual sumogallery object
  89. SG: {imgs: {}, sindex: 0, //title:'',close:'',body:'',left:'',right:'',thumbs:''
  90. reset: function () {
  91. this.sindex = 0;
  92. },
  93. setEvents: function () {
  94. var O = this;
  95. //close button on top right;
  96. O.close.on('click.sg', function () {
  97. $('.SumoGallery').fadeOut(300, function () {
  98. $('.SumoGallery').remove();
  99. });
  100. $(document).off('keydown.sg');
  101. });
  102. O.left.on('click.sg', function () {
  103. O.prev();
  104. });
  105. O.right.on('click.sg', function () {
  106. O.next();
  107. });
  108. O.thumbs.find('li>a').on('click.sg', function () {
  109. O.select($(this).parent().index());
  110. });
  111. //touch events
  112. O.body.find('a').on('swipeleft', function () {
  113. O.next();
  114. });
  115. O.body.find('a').on('swiperight', function () {
  116. O.prev();
  117. });
  118. O.arrow.on('click', function () {
  119. O._SG.toggleClass('nothumbs');
  120. })
  121. //connect keyboard keys
  122. $(document).on('keydown.sg', function (e) {
  123. switch (e.which) {
  124. case 37: // left
  125. O.prev();
  126. break;
  127. case 39: // right
  128. O.next();
  129. break;
  130. case 27: // esc
  131. O.close.trigger('click.sg');
  132. break;
  133. default:
  134. return; // exit this handler for other keys
  135. }
  136. e.preventDefault(); // prevent the default action (scroll / move caret)
  137. });
  138. },
  139. // set selected an image in gallery by a given index.
  140. select: function (i) {
  141. var O = this;
  142. if (i > O.imgs.length - 1 || i < 0) return;
  143. O.thumbs.children('li.sel').removeClass('sel');
  144. O.thumbs.children('li').eq(i).addClass('sel')
  145. //put sel thumb in center
  146. var centre = O.thumbs.width() / 2 - O.thumbs.children('li').width() / 2
  147. O.thumbs.stop().animate({scrollLeft: O.thumbs.children('li.sel').index() * O.thumbs.children('li').outerWidth(true) - centre});
  148. var _new = O.body.children('a').eq(i)//.addClass('sel');
  149. var _old = O.body.children('a.sel')//.removeClass('sel');
  150. if (!_old.length)_new.addClass('sel anim');
  151. if (i < this.sindex) { //slide to right
  152. _old.addClass('right');
  153. setTimeout(function () {
  154. _old.removeClass('right sel anim')
  155. }, 300);
  156. _new.removeClass('anim right').addClass('sel left');
  157. setTimeout(function () {
  158. _new.addClass('anim').removeClass('left')
  159. }, 5);
  160. }
  161. else if (i > this.sindex) { //slide to left
  162. _old.addClass('left');
  163. setTimeout(function () {
  164. _old.removeClass('left sel anim')
  165. }, 300);
  166. _new.removeClass('anim left').addClass('sel right');
  167. setTimeout(function () {
  168. _new.addClass('anim').removeClass('right')
  169. }, 5);
  170. }
  171. this.sindex = i;
  172. //set title of image
  173. O.title.text(_new.children('img').attr('title'));
  174. //set counts depending on settings.
  175. if (sgallery.settings.showImgCount) O.count.text('image ' + (i + 1) + ' of ' + O.imgs.length);
  176. O.left.toggleClass('last-item', i < 1);
  177. O.right.toggleClass('last-item', i > O.imgs.length - 2);
  178. },
  179. next: function () {
  180. this.select(this.sindex + 1);
  181. },
  182. prev: function () {
  183. this.select(this.sindex - 1);
  184. },
  185. init: function () {
  186. var O = this;
  187. O.setEvents();
  188. O.select(O.sindex);
  189. }
  190. },
  191. init: function () {
  192. var O = this;
  193. this.bindEvents();
  194. }
  195. };
  196. sgallery.init();
  197. }(jQuery));