zoom-slideshow.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Zoom SlideShow - jQuery Plugin
  2. // Copyright (c) 2013, Vincent CIBELLI
  3. // Licenced under MIT Licence
  4. (function($) {
  5. $.fn.extend({
  6. // Déclaration du plugin
  7. setZoomPicture: function(options) {
  8. //Déclaration des paramètres par défaut
  9. var defaults = {
  10. thumbsContainer: null,
  11. prevContainer: null,
  12. nextContainer: null,
  13. zoomContainer: null,
  14. zoomLevel: 2,
  15. loadMsg: 'Loading...'
  16. };
  17. // Initialisation de la collection de paramètres
  18. var _options = $.extend(defaults, options);
  19. // Création de la classe de gestion
  20. var diaporama = {
  21. _opt: null,
  22. _totalImg: 0,
  23. _currentImg: 0,
  24. _thumbSize: 0,
  25. init: function(opt) {
  26. this._opt = opt;
  27. this._totalImg = $(this._opt.thumbsContainer).find("img").length - 1;
  28. this._currentImg = 0;
  29. this._thumbSize = $(this._opt.thumbsContainer).find("img").first().outerWidth(true) + 3;
  30. // Initialisation des événements 'click' sur les miniatures et les boutons
  31. this._attachThumbsEvent();
  32. // Initialisation des effets de zoom
  33. this._setZoomEffects();
  34. // Affichage de la première image de la banque
  35. this._showPicture($(this._opt.thumbsContainer).find("img").first());
  36. },
  37. _attachThumbsEvent: function() {
  38. var me = this;
  39. $(this._opt.thumbsContainer).find("img").click(function(e) {
  40. me._setDefault();
  41. me._currentImg = $('img').index(this) - 1;
  42. var thumbImg = $(this);
  43. me._animateThumbs(e);
  44. // Remplacement de l'image affiché par la nouvelle
  45. me._showPicture($(thumbImg));
  46. });
  47. $(this._opt.prevContainer).click(function(e) {me._previous(e);});
  48. $(this._opt.nextContainer).click(function(e) {me._next(e);});
  49. },
  50. _setDefault: function() {
  51. $(this._opt.zoomContainer)
  52. .unbind('mousemove')
  53. .hide();
  54. $(this).css('cursor', 'default');
  55. },
  56. _previous: function(pos) {
  57. if (this._currentImg > 0) {
  58. this._currentImg--;
  59. this._animateThumbs(pos);
  60. this._showPicture($(this._opt.thumbsContainer).find("img")[this._currentImg]);
  61. }
  62. },
  63. _next: function(pos) {
  64. if (this._currentImg < this._totalImg) {
  65. this._currentImg++;
  66. this._animateThumbs(pos);
  67. this._showPicture($(this._opt.thumbsContainer).find("img")[this._currentImg]);
  68. }
  69. },
  70. _animateThumbs: function(pos) {
  71. var me = this;
  72. if (($(document).width() / 2) < pos.pageX) {
  73. $(me._opt.thumbsContainer).animate(
  74. {scrollLeft : Math.max(0, $(me._opt.thumbsContainer).scrollLeft() + me._thumbSize)},
  75. 800
  76. );
  77. }
  78. else {
  79. $(me._opt.thumbsContainer).animate(
  80. {scrollLeft : Math.max(0, $(me._opt.thumbsContainer).scrollLeft() - me._thumbSize)},
  81. 800
  82. );
  83. }
  84. },
  85. _showPicture: function(thumb) {
  86. $(this._opt.thumbsContainer).find('img').css('background', '#fff');
  87. $(thumb).css('background', '#ccc');
  88. var _img = new Image();
  89. _img.src = $(thumb).attr('src');
  90. var me = this;
  91. $(this._opt.jObject).find("img").fadeOut('slow', function() {
  92. var img = $(this);
  93. $(img).attr('src', $(thumb).attr('src'))
  94. .attr('alt', $(thumb).attr('alt'));
  95. $(me._opt.jObject).animate(
  96. { height: (_img.height / me._opt.zoomLevel) + 'px' },
  97. 500,
  98. function() {
  99. $(img).width(_img.width / me._opt.zoomLevel)
  100. .fadeIn('slow');
  101. }
  102. );
  103. });
  104. },
  105. _setZoomEffects: function() {
  106. var me = this;
  107. $(this._opt.jObject).find('img')
  108. .bind('mouseover', function(e) {
  109. var imgObject = $(this);
  110. me._setZoomPosition(e);
  111. $(me._opt.zoomContainer)
  112. .css('cursor', 'none')
  113. .show();
  114. $(document).bind('mousemove', function(pos) {
  115. me._moveZoom(imgObject, pos);
  116. });
  117. });
  118. },
  119. _moveZoom: function(img, pos) {
  120. if (pos.pageX < $(img).position().left ||
  121. pos.pageX > $(img).position().left + $(img).width() ||
  122. pos.pageY < $(img).position().top ||
  123. pos.pageY > $(img).position().top + $(img).height() ) {
  124. this._setDefault();
  125. }
  126. else {
  127. var delta = 50 * (this._opt.zoomLevel - 1);
  128. this._setZoomPosition(pos);
  129. $(this._opt.zoomContainer)
  130. .css('background-image', "url('" + $(img).attr('src') + "')")
  131. .css('background-position',
  132. (((pos.pageX - 50 - $(img).position().left) * this._opt.zoomLevel * -1) - delta).toString() + "px " +
  133. (((pos.pageY - 50 - $(img).position().top) * this._opt.zoomLevel * -1) - delta).toString() + "px");
  134. }
  135. },
  136. _setZoomPosition: function(_pos) {
  137. $(this._opt.zoomContainer)
  138. .css('top', _pos.pageY - 50)
  139. .css('left', _pos.pageX - 50);
  140. }
  141. };
  142. // Retourne chaque objet jQuery
  143. return this.each(function() {
  144. // Ajoute l'objet jQuery courant à la collection 'options'
  145. _options.jObject = $(this);
  146. // Création d'un message d'attente pendant le chargement
  147. $(this).find('img').hide();
  148. $(this).prepend('<div id="loading">' + _options.loadMsg + '</div>');
  149. $('#loading').css('line-height', $(this).css('height'))
  150. .queue(function(loop) {
  151. $(this).fadeIn(1000).delay(500).fadeOut(1000);
  152. $(this).queue(arguments.callee);
  153. loop();
  154. });
  155. // Chargement de la classe diaporama a la fin du téléchargement
  156. $(window).load(function() {
  157. $('#loading').remove();
  158. // Initialisation de la classe 'diaporama'
  159. diaporama.init(_options);
  160. });
  161. });
  162. }
  163. });
  164. })(jQuery);