jquery.capty.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * jQuery Capty - A Caption Plugin - http://wbotelhos.com/capty
  3. * ---------------------------------------------------------------------------------
  4. *
  5. * jQuery Capty is a plugin that creates captions over the images.
  6. *
  7. * Licensed under The MIT License
  8. *
  9. * @version 0.2.1
  10. * @since 12.18.2010
  11. * @author Washington Botelho dos Santos
  12. * @documentation wbotelhos.com/capty
  13. * @twitter twitter.com/wbotelhos
  14. * @license opensource.org/licenses/mit-license.php MIT
  15. * @package jQuery Plugins
  16. *
  17. * Usage with default values:
  18. * ---------------------------------------------------------------------------------
  19. * $('#capty').capty();
  20. *
  21. * <img id="capty" src="image.jpg" alt="Super Mario Bros.&reg;" width="150" height="150"/>
  22. *
  23. */
  24. ;(function($) {
  25. $.fn.capty = function(settings) {
  26. var options = $.extend({}, $.fn.capty.defaults, settings);
  27. if (this.length == 0) {
  28. debug('Selector invalid or missing!');
  29. return;
  30. } else if (this.length > 1) {
  31. return this.each(function() {
  32. $.fn.capty.apply($(this), [settings]);
  33. });
  34. }
  35. var $this = $(this),
  36. name = $this.attr('name'),
  37. $caption = $('<div class="' + options.cCaption + '"/>'),
  38. $elem = $this;
  39. if ($this.parent().is('a')) {
  40. $elem = $this.parent();
  41. }
  42. var $image = $elem.wrap('<div class="' + options.cImage + '"/>').parent(),
  43. $wrapper = $image.wrap('<div class="' + options.cWrapper + '"/>').parent();
  44. $wrapper.css({
  45. height: $this.height(),
  46. overflow: 'hidden',
  47. position: 'relative',
  48. width: $this.width()
  49. });
  50. $caption.css({
  51. height: options.height,
  52. opacity: options.opacity,
  53. position: 'relative'
  54. })
  55. .click(function(evt) {
  56. evt.stopPropagation();
  57. })
  58. .appendTo($wrapper);
  59. if (name) {
  60. var $content = $(name);
  61. if ($content.length) {
  62. $content.appendTo($caption);
  63. } else {
  64. $caption.html('<span style="color: #F00;">Content invalid or missing!</span>');
  65. }
  66. } else {
  67. $caption.html($this.attr('alt'));
  68. }
  69. if (options.prefix) {
  70. $caption.prepend(options.prefix);
  71. }
  72. if (options.sufix) {
  73. $caption.append(options.sufix);
  74. }
  75. if (options.animation == 'slide') {
  76. $wrapper.hover(
  77. function() {
  78. $caption.animate({ top: (-1 * options.height) }, { duration: options.speed, queue: false });
  79. },
  80. function() {
  81. $caption.animate({ top: 0 }, { duration: options.speed, queue: false });
  82. }
  83. );
  84. } else if (options.animation == 'fade') {
  85. $caption.css({
  86. opacity: 0,
  87. top: (-1 * options.height) + 'px'
  88. });
  89. $wrapper.hover(
  90. function() {
  91. $caption.stop().animate({ opacity: options.opacity }, options.speed);
  92. },
  93. function() {
  94. $caption.stop().animate({ opacity: 0 }, options.speed);
  95. }
  96. );
  97. } else if (options.animation == 'fixed') {
  98. $caption.css('top', (-1 * options.height) + 'px');
  99. } else {
  100. debug($this.attr('id') + ': invalid animation!');
  101. }
  102. return $this;
  103. };
  104. function debug(message) {
  105. if (window.console && window.console.log) {
  106. window.console.log(message);
  107. }
  108. };
  109. $.fn.capty.defaults = {
  110. animation: 'slide',
  111. cCaption: 'capty-caption',
  112. cImage: 'capty-image',
  113. cWrapper: 'capty-wrapper',
  114. height: 30,
  115. opacity: .7,
  116. prefix: '',
  117. speed: 200,
  118. sufix: ''
  119. };
  120. })(jQuery);