jquery.fancybox-thumbs.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*!
  2. * Thumbnail helper for fancyBox
  3. * version: 1.0.7 (Mon, 01 Oct 2012)
  4. * @requires fancyBox v2.0 or later
  5. *
  6. * Usage:
  7. * $(".fancybox").fancybox({
  8. * helpers : {
  9. * thumbs: {
  10. * width : 50,
  11. * height : 50
  12. * }
  13. * }
  14. * });
  15. *
  16. */
  17. (function ($) {
  18. //Shortcut for fancyBox object
  19. var F = $.fancybox;
  20. //Add helper object
  21. F.helpers.thumbs = {
  22. defaults : {
  23. width : 50, // thumbnail width
  24. height : 50, // thumbnail height
  25. position : 'bottom', // 'top' or 'bottom'
  26. source : function ( item ) { // function to obtain the URL of the thumbnail image
  27. var href;
  28. if (item.element) {
  29. href = $(item.element).find('img').attr('src');
  30. }
  31. if (!href && item.type === 'image' && item.href) {
  32. href = item.href;
  33. }
  34. return href;
  35. }
  36. },
  37. wrap : null,
  38. list : null,
  39. width : 0,
  40. init: function (opts, obj) {
  41. var that = this,
  42. list,
  43. thumbWidth = opts.width,
  44. thumbHeight = opts.height,
  45. thumbSource = opts.source;
  46. //Build list structure
  47. list = '';
  48. for (var n = 0; n < obj.group.length; n++) {
  49. list += '<li><a style="width:' + thumbWidth + 'px;height:' + thumbHeight + 'px;" href="javascript:jQuery.fancybox.jumpto(' + n + ');"></a></li>';
  50. }
  51. this.wrap = $('<div id="fancybox-thumbs"></div>').addClass(opts.position).appendTo('body');
  52. this.list = $('<ul>' + list + '</ul>').appendTo(this.wrap);
  53. //Load each thumbnail
  54. $.each(obj.group, function (i) {
  55. var href = thumbSource( obj.group[ i ] );
  56. if (!href) {
  57. return;
  58. }
  59. $("<img />").load(function () {
  60. var width = this.width,
  61. height = this.height,
  62. widthRatio, heightRatio, parent;
  63. if (!that.list || !width || !height) {
  64. return;
  65. }
  66. //Calculate thumbnail width/height and center it
  67. widthRatio = width / thumbWidth;
  68. heightRatio = height / thumbHeight;
  69. parent = that.list.children().eq(i).find('a');
  70. if (widthRatio >= 1 && heightRatio >= 1) {
  71. if (widthRatio > heightRatio) {
  72. width = Math.floor(width / heightRatio);
  73. height = thumbHeight;
  74. } else {
  75. width = thumbWidth;
  76. height = Math.floor(height / widthRatio);
  77. }
  78. }
  79. $(this).css({
  80. width : width,
  81. height : height,
  82. top : Math.floor(thumbHeight / 2 - height / 2),
  83. left : Math.floor(thumbWidth / 2 - width / 2)
  84. });
  85. parent.width(thumbWidth).height(thumbHeight);
  86. $(this).hide().appendTo(parent).fadeIn(300);
  87. }).attr('src', href);
  88. });
  89. //Set initial width
  90. this.width = this.list.children().eq(0).outerWidth(true);
  91. this.list.width(this.width * (obj.group.length + 1)).css('left', Math.floor($(window).width() * 0.5 - (obj.index * this.width + this.width * 0.5)));
  92. },
  93. beforeLoad: function (opts, obj) {
  94. //Remove self if gallery do not have at least two items
  95. if (obj.group.length < 2) {
  96. obj.helpers.thumbs = false;
  97. return;
  98. }
  99. //Increase bottom margin to give space for thumbs
  100. obj.margin[ opts.position === 'top' ? 0 : 2 ] += ((opts.height) + 15);
  101. },
  102. afterShow: function (opts, obj) {
  103. //Check if exists and create or update list
  104. if (this.list) {
  105. this.onUpdate(opts, obj);
  106. } else {
  107. this.init(opts, obj);
  108. }
  109. //Set active element
  110. this.list.children().removeClass('active').eq(obj.index).addClass('active');
  111. },
  112. //Center list
  113. onUpdate: function (opts, obj) {
  114. if (this.list) {
  115. this.list.stop(true).animate({
  116. 'left': Math.floor($(window).width() * 0.5 - (obj.index * this.width + this.width * 0.5))
  117. }, 150);
  118. }
  119. },
  120. beforeClose: function () {
  121. if (this.wrap) {
  122. this.wrap.remove();
  123. }
  124. this.wrap = null;
  125. this.list = null;
  126. this.width = 0;
  127. }
  128. }
  129. }(jQuery));