jquery.fancybox-thumbs.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 el = obj.group[ i ],
  56. href = thumbSource( el );
  57. if (!href) {
  58. return;
  59. }
  60. $("<img />").load(function () {
  61. var width = this.width,
  62. height = this.height,
  63. widthRatio, heightRatio, parent;
  64. if (!that.list || !width || !height) {
  65. return;
  66. }
  67. //Calculate thumbnail width/height and center it
  68. widthRatio = width / thumbWidth;
  69. heightRatio = height / thumbHeight;
  70. parent = that.list.children().eq(i).find('a');
  71. if (widthRatio >= 1 && heightRatio >= 1) {
  72. if (widthRatio > heightRatio) {
  73. width = Math.floor(width / heightRatio);
  74. height = thumbHeight;
  75. } else {
  76. width = thumbWidth;
  77. height = Math.floor(height / widthRatio);
  78. }
  79. }
  80. $(this).css({
  81. width : width,
  82. height : height,
  83. top : Math.floor(thumbHeight / 2 - height / 2),
  84. left : Math.floor(thumbWidth / 2 - width / 2)
  85. });
  86. parent.width(thumbWidth).height(thumbHeight);
  87. $(this).hide().appendTo(parent).fadeIn(300);
  88. })
  89. .attr('src', href)
  90. .attr('title', el.title);
  91. });
  92. //Set initial width
  93. this.width = this.list.children().eq(0).outerWidth(true);
  94. 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)));
  95. },
  96. beforeLoad: function (opts, obj) {
  97. //Remove self if gallery do not have at least two items
  98. if (obj.group.length < 2) {
  99. obj.helpers.thumbs = false;
  100. return;
  101. }
  102. //Increase bottom margin to give space for thumbs
  103. obj.margin[ opts.position === 'top' ? 0 : 2 ] += ((opts.height) + 15);
  104. },
  105. afterShow: function (opts, obj) {
  106. //Check if exists and create or update list
  107. if (this.list) {
  108. this.onUpdate(opts, obj);
  109. } else {
  110. this.init(opts, obj);
  111. }
  112. //Set active element
  113. this.list.children().removeClass('active').eq(obj.index).addClass('active');
  114. },
  115. //Center list
  116. onUpdate: function (opts, obj) {
  117. if (this.list) {
  118. this.list.stop(true).animate({
  119. 'left': Math.floor($(window).width() * 0.5 - (obj.index * this.width + this.width * 0.5))
  120. }, 150);
  121. }
  122. },
  123. beforeClose: function () {
  124. if (this.wrap) {
  125. this.wrap.remove();
  126. }
  127. this.wrap = null;
  128. this.list = null;
  129. this.width = 0;
  130. }
  131. }
  132. }(jQuery));