jquery.chocolat.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. Inspired by the lightbox plugin adapted to jquery by Leandro Vieira Pinho (http://leandrovieira.com)
  3. @author : Nicolas Turlais : nicolas-at-insipi.de
  4. @version : V0.3.1 - June 2012
  5. @license : Licensed under CCAttribution-ShareAlike
  6. @website : http://chocolat.insipi.de
  7. */
  8. (function ($) {
  9. images = [];
  10. var calls = 0;
  11. $.fn.Chocolat = function (settings) {
  12. settings = $.extend({
  13. container: $('body'),
  14. displayAsALink: false,
  15. linkImages: true,
  16. linksContainer: 'Choco_links_container',
  17. overlayOpacity: 0.9,
  18. overlayColor: '#fff',
  19. fadeInOverlayduration: 500,
  20. fadeInImageduration: 500,
  21. fadeOutImageduration: 500,
  22. vache: true,
  23. separator1: ' | ',
  24. separator2: '/',
  25. leftImg: 'images/left.gif',
  26. rightImg: 'images/right.gif',
  27. closeImg: 'images/close.gif',
  28. loadingImg: 'images/loading.gif',
  29. currentImage: 0,
  30. setIndex: 0,
  31. setTitle: '',
  32. lastImage: 0
  33. }, settings);
  34. calls++;
  35. settings.setIndex = calls;
  36. images[settings.setIndex] = [];
  37. //images:
  38. this.each(function (index) {
  39. if (index == 0 && settings.linkImages && settings.setTitle == '') {
  40. settings.setTitle = isSet($(this).attr('rel'), ' ');
  41. }
  42. $(this).each(function () {
  43. images[settings.setIndex]['displayAsALink'] = settings.displayAsALink;
  44. images[settings.setIndex][index] = [];
  45. images[settings.setIndex][index]['adress'] = isSet($(this).attr('href'), ' ');
  46. images[settings.setIndex][index]['caption'] = isSet($(this).attr('title'), ' ');
  47. if (!settings.displayAsALink) {
  48. $(this).unbind('click').bind('click', {
  49. id: settings.setIndex,
  50. nom: settings.setTitle,
  51. i: index
  52. }, _initialise);
  53. }
  54. })
  55. });
  56. //setIndex:
  57. for (var i = 0; i < images[settings.setIndex].length; i++) {
  58. if (images[settings.setIndex]['displayAsALink']) {
  59. if ($('#' + settings.linksContainer).size() == 0) {
  60. this.filter(":first").before('<ul id="' + settings.linksContainer + '"></ul>');
  61. }
  62. $('#' + settings.linksContainer).append('<li><a href="#" id="Choco_numsetIndex_' + settings.setIndex + '" class="Choco_link">' + settings.setTitle + '</a></li>');
  63. e = this.parent();
  64. $(this).remove();
  65. if ($.trim(e.html()) == "") {//If parent empty : remove it
  66. e.remove();
  67. }
  68. return $('#Choco_numsetIndex_' + settings.setIndex).unbind('click').bind('click', {
  69. id: settings.setIndex,
  70. nom: settings.setTitle,
  71. i: settings.currentImage
  72. }, _initialise);
  73. }
  74. }
  75. function _initialise(event) {
  76. settings.currentImage = event.data.i;
  77. settings.setIndex = event.data.id;
  78. settings.setTitle = event.data.nom;
  79. settings.lastImage = images[settings.setIndex].length - 1;
  80. showChocolat();
  81. return false;
  82. }
  83. function _interface() {
  84. //html
  85. clear();
  86. settings.container.append('<div id="Choco_overlay"></div><div id="Choco_content"><div id="Choco_close"></div><div id="Choco_loading"></div><div id="Choco_container_photo"><img id="Choco_bigImage" src="" /></div><div id="Choco_container_description"><span id="Choco_container_title"></span><span id="Choco_container_via"></span></div><div id="Choco_left_arrow" class="Choco_arrows"></div><div id="Choco_right_arrow" class="Choco_arrows"></div></div>');
  87. $('#Choco_left_arrow').css('background-image', 'url(' + settings.leftImg + ')');
  88. $('#Choco_right_arrow').css('background-image', 'url(' + settings.rightImg + ')');
  89. $('#Choco_close').css('background-image', 'url(' + settings.closeImg + ')');
  90. $('#Choco_loading').css('background-image', 'url(' + settings.loadingImg + ')');
  91. if (settings.container.get(0).nodeName.toLowerCase() !== 'body') {
  92. settings.container.css({'position': 'relative', 'overflow': 'hidden', 'line-height': 'normal'});//yes, yes
  93. $('#Choco_content').css('position', 'relative');
  94. $('#Choco_overlay').css('position', 'absolute');
  95. }
  96. //events
  97. $(document).unbind('keydown').bind('keydown', function (e) {
  98. switch (e.keyCode) {
  99. case 37:
  100. changePageChocolat(-1);
  101. break;
  102. case 39:
  103. changePageChocolat(1);
  104. break;
  105. case 27:
  106. close();
  107. break;
  108. }
  109. ;
  110. });
  111. if (settings.vache) {
  112. $('#Choco_overlay').click(function () {
  113. close();
  114. return false;
  115. });
  116. }
  117. $('#Choco_left_arrow').unbind().bind('click', function () {
  118. changePageChocolat(-1);
  119. return false;
  120. });
  121. $('#Choco_right_arrow').unbind().bind('click', function () {
  122. changePageChocolat(1);
  123. return false;
  124. });
  125. $('#Choco_close').unbind().bind('click', function () {
  126. close();
  127. return false;
  128. });
  129. $(window).resize(function () {
  130. load(settings.currentImage, true);
  131. });
  132. }
  133. function showChocolat() {
  134. _interface();
  135. load(settings.currentImage, false);
  136. $('#Choco_overlay')
  137. .css({
  138. 'background-color': settings.overlayColor,
  139. 'opacity': settings.overlayOpacity
  140. })
  141. .fadeIn(settings.fadeInOverlayduration);
  142. $('#Choco_content').fadeIn(settings.fadeInImageduration, function () {
  143. });
  144. }
  145. function load(image, resize) {
  146. settings.currentImage = image;
  147. $('#Choco_loading').fadeIn(settings.fadeInImageduration);
  148. var imgPreloader = new Image();
  149. imgPreloader.onload = function () {
  150. $('#Choco_bigImage').attr('src', images[settings.setIndex][settings.currentImage]['adress']);
  151. var ajustees = iWantThePerfectImageSize(imgPreloader.height, imgPreloader.width);
  152. ChoColat(ajustees['hauteur'], ajustees['largeur'], resize);
  153. $('#Choco_loading').stop().fadeOut(settings.fadeOutImageduration);
  154. };
  155. imgPreloader.src = images[settings.setIndex][settings.currentImage]['adress'];
  156. preload();
  157. upadteDescription();
  158. }
  159. function changePageChocolat(signe) {
  160. if (!settings.linkImages || (settings.currentImage == 0 && signe == -1) || (settings.currentImage == settings.lastImage && signe == 1)) {
  161. return false;
  162. } else {
  163. //$('#Choco_container_description').fadeTo(settings.fadeOutImageduration,0); making a weird bug with firefox 17
  164. $('#Choco_container_description').css('visibility', 'hidden');
  165. $('#Choco_bigImage').fadeTo(settings.fadeOutImageduration, 0, function () {
  166. load(settings.currentImage + parseInt(signe), false);
  167. });
  168. }
  169. }
  170. function ChoColat(hauteur_image, largeur_image, resize) {
  171. if (resize) {
  172. $('#Choco_container_photo, #Choco_content, #Choco_bigImage').stop(true, false).css({'overflow': 'visible'});
  173. $('#Choco_bigImage').animate({
  174. 'height': hauteur_image + 'px',
  175. 'width': largeur_image + 'px'
  176. }, settings.fadeInImageduration);
  177. }
  178. $('#Choco_container_photo').animate({
  179. 'height': hauteur_image,
  180. 'width': largeur_image
  181. }, settings.fadeInImageduration);
  182. $('#Choco_content').animate({
  183. 'height': hauteur_image,
  184. 'width': largeur_image,
  185. 'marginLeft': -largeur_image / 2,
  186. 'marginTop': -(hauteur_image) / 2
  187. }, settings.fadeInImageduration, 'swing', function () {
  188. $('#Choco_bigImage').fadeTo(settings.fadeInImageduration, 1).height(hauteur_image).width(largeur_image);
  189. if (!resize) {
  190. arrowsManaging();
  191. //$('#Choco_container_description').fadeTo(settings.fadeInImageduration,1); making a weird bug with firefox 17
  192. $('#Choco_container_description').css('visibility', 'visible');
  193. $('#Choco_close').fadeIn(settings.fadeInImageduration);
  194. }
  195. }).css('overflow', 'visible');
  196. }
  197. function arrowsManaging() {
  198. if (settings.linkImages) {
  199. var what = ['Choco_right_arrow', 'Choco_left_arrow'];
  200. for (var i = 0; i < what.length; i++) {
  201. hide = false;
  202. if (what[i] == 'Choco_right_arrow' && settings.currentImage == settings.lastImage) {
  203. hide = true;
  204. $('#' + what[i]).fadeOut(300);
  205. } else if (what[i] == 'Choco_left_arrow' && settings.currentImage == 0) {
  206. hide = true;
  207. $('#' + what[i]).fadeOut(300);
  208. }
  209. if (!hide) {
  210. $('#' + what[i]).fadeIn(settings.fadeOutImageduration);
  211. }
  212. }
  213. }
  214. }
  215. function preload() {
  216. if (settings.currentImage !== settings.lastImage) {
  217. i = new Image;
  218. z = settings.currentImage + 1;
  219. i.src = images[settings.setIndex][z]['adress'];
  220. }
  221. }
  222. function upadteDescription() {
  223. var current = settings.currentImage + 1;
  224. var last = settings.lastImage + 1;
  225. $('#Choco_container_title').html(images[settings.setIndex][settings.currentImage]['caption']);
  226. $('#Choco_container_via').html(settings.setTitle + settings.separator1 + current + settings.separator2 + last);
  227. }
  228. function isSet(variable, defaultValue) {
  229. // return variable === undefined ? defaultValue : variable; ?
  230. if (variable === undefined) {
  231. return defaultValue;
  232. } else {
  233. return variable;
  234. }
  235. }
  236. function iWantThePerfectImageSize(himg, limg) {
  237. //28% = 14% + 14% margin
  238. var lblock = limg + (limg * 28 / 100);
  239. var heightDescAndClose = $('#Choco_container_description').height() + $('#Choco_close').height();
  240. var hblock = himg + heightDescAndClose;
  241. var k = limg / himg;
  242. var kk = himg / limg;
  243. if (settings.container.get(0).nodeName.toLowerCase() == 'body') {
  244. windowHeight = $(window).height();
  245. windowWidth = $(window).width();
  246. } else {
  247. windowHeight = settings.container.height();
  248. windowWidth = settings.container.width();
  249. }
  250. notFitting = true;
  251. while (notFitting) {
  252. var lblock = limg + (limg * 28 / 100);
  253. var hblock = himg + heightDescAndClose;
  254. if (lblock > windowWidth) {
  255. limg = windowWidth * 100 / 128;
  256. himg = kk * limg;
  257. } else if (hblock > windowHeight) {
  258. himg = (windowHeight - heightDescAndClose);
  259. limg = k * himg;
  260. } else {
  261. notFitting = false;
  262. }
  263. ;
  264. }
  265. ;
  266. return {
  267. largeur: limg,
  268. hauteur: himg
  269. };
  270. }
  271. function clear() {
  272. $('#Choco_overlay').remove();
  273. $('#Choco_content').remove();
  274. }
  275. function close() {
  276. $('#Choco_overlay').fadeOut(900, function () {
  277. $('#Choco_overlay').remove()
  278. });
  279. $('#Choco_content').fadeOut(500, function () {
  280. $('#Choco_content').remove()
  281. });
  282. settings.currentImage = 0;
  283. }
  284. };
  285. })(jQuery);