You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
5.0 KiB

2 years ago
  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 />").on("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));