jquery.fancybox-thumbs.js 3.6 KB

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