1 | // jQuery Captify Plugin |
---|
2 | // Copyright (C) 2009 Brian Reavis |
---|
3 | // Licenced under the MIT license |
---|
4 | // $Date: 2009-11-30 [Mon, 30 Nov 2009] $ |
---|
5 | jQuery.fn.extend({ |
---|
6 | captify: function(uo) { |
---|
7 | var o = $.extend({ |
---|
8 | speedOver: 'fast', // speed of the mouseover effect |
---|
9 | speedOut: 'normal', // speed of the mouseout effect |
---|
10 | hideDelay: 500, // how long to delay the hiding of the caption after mouseout (ms) |
---|
11 | animation: 'slide', // 'fade', 'slide', 'always-on' |
---|
12 | prefix: '', // text/html to be placed at the beginning of every caption |
---|
13 | opacity: '0.45', // opacity of the caption on mouse over |
---|
14 | className: 'caption-bottom', // the name of the CSS class to apply to the caption box |
---|
15 | position: 'bottom', // position of the caption (top or bottom) |
---|
16 | spanWidth: '100%' // caption span % of the image |
---|
17 | }, uo); |
---|
18 | $(this).each(function() { |
---|
19 | var img = this; |
---|
20 | $(this).load(function() { |
---|
21 | if (img.hasInit){ return false; } |
---|
22 | img.hasInit = true; |
---|
23 | var over_caption = false; |
---|
24 | var over_img = false; |
---|
25 | |
---|
26 | //pull the label from another element if there is a |
---|
27 | //valid element id inside the rel="..." attribute, otherwise, |
---|
28 | //just use the text in the alt="..." attribute. |
---|
29 | var captionLabelSrc = $('#' + $(this).attr('rel')); |
---|
30 | var captionLabelHTML = !captionLabelSrc.length ? $(this).attr('alt') : captionLabelSrc.html(); |
---|
31 | captionLabelSrc.remove(); |
---|
32 | var toWrap = this.parent && this.parent.tagName == 'a' ? this.parent : $(this); |
---|
33 | |
---|
34 | var wrapper = |
---|
35 | toWrap.wrap('<div></div>').parent() |
---|
36 | .css({ |
---|
37 | overflow: 'hidden', |
---|
38 | padding: 0, |
---|
39 | fontSize: 0.1 |
---|
40 | }) |
---|
41 | .addClass('caption-wrapper') |
---|
42 | .width($(this).width()) |
---|
43 | .height($(this).height()); |
---|
44 | |
---|
45 | //transfer the margin and border properties from the image to the wrapper |
---|
46 | $.map(['top', 'right', 'bottom', 'left'], function(i) { |
---|
47 | wrapper.css('margin-' + i, $(img).css('margin-' + i)); |
---|
48 | $.map(['style', 'width', 'color'], function(j) { |
---|
49 | var key = 'border-' + i + '-' + j; |
---|
50 | wrapper.css(key, $(img).css(key)); |
---|
51 | }); |
---|
52 | }); |
---|
53 | $(img).css({ border: '0 none' }); |
---|
54 | |
---|
55 | //create two consecutive divs, one for the semi-transparent background, |
---|
56 | //and other other for the fully-opaque label |
---|
57 | var caption = $('div:last', wrapper.append('<div></div>')) |
---|
58 | .addClass(o.className); |
---|
59 | |
---|
60 | var captionContent = $('div:last', wrapper.append('<div></div>')) |
---|
61 | .addClass(o.className) |
---|
62 | .append(o.prefix) |
---|
63 | .append(captionLabelHTML); |
---|
64 | |
---|
65 | //override hiding from CSS, and reset all margins (which could have been inherited) |
---|
66 | $('*', wrapper).css({ margin: 0 }).show(); |
---|
67 | |
---|
68 | //ensure the background is on bottom |
---|
69 | var captionPositioning = jQuery.browser.msie ? 'static' : 'relative'; |
---|
70 | caption.css({ |
---|
71 | zIndex: 1, |
---|
72 | position: captionPositioning, |
---|
73 | opacity: o.animation == 'fade' ? 0 : o.opacity, |
---|
74 | width: o.spanWidth |
---|
75 | }); |
---|
76 | |
---|
77 | if (o.position == 'bottom'){ |
---|
78 | var vLabelOffset = |
---|
79 | parseInt(caption.css('border-top-width').replace('px', '')) + |
---|
80 | parseInt(captionContent.css('padding-top').replace('px', '')) - 1; |
---|
81 | captionContent.css('paddingTop', vLabelOffset); |
---|
82 | } |
---|
83 | //clear the backgrounds/borders from the label, and make it fully-opaque |
---|
84 | captionContent.css({ |
---|
85 | position: captionPositioning, |
---|
86 | zIndex: 2, |
---|
87 | background: 'none', |
---|
88 | border: '0 none', |
---|
89 | opacity: o.animation == 'fade' ? 0 : 1, |
---|
90 | width: o.spanWidth |
---|
91 | }); |
---|
92 | caption.width(captionContent.outerWidth()); |
---|
93 | caption.height(captionContent.height()); |
---|
94 | |
---|
95 | // represents caption margin positioning for hide and show states |
---|
96 | var topBorderAdj = o.position == 'bottom' && jQuery.browser.msie ? -4 : 0; |
---|
97 | var captionPosition = o.position == 'top' |
---|
98 | ? { hide: -$(img).height() - caption.outerHeight() - 1, show: -$(img).height() } |
---|
99 | : { hide: 0, show: -caption.outerHeight() + topBorderAdj }; |
---|
100 | |
---|
101 | //pull the label up on top of the background |
---|
102 | captionContent.css('marginTop', -caption.outerHeight()); |
---|
103 | caption.css('marginTop', captionPosition[o.animation == 'fade' || o.animation == 'always-on' ? 'show' : 'hide']); |
---|
104 | |
---|
105 | //function to push the caption out of view |
---|
106 | var cHide = function() { |
---|
107 | if (!over_caption && !over_img){ |
---|
108 | var props = o.animation == 'fade' |
---|
109 | ? { opacity: 0 } |
---|
110 | : { marginTop: captionPosition.hide }; |
---|
111 | caption.animate(props, o.speedOut); |
---|
112 | if (o.animation == 'fade'){ |
---|
113 | captionContent.animate({opacity: 0}, o.speedOver); |
---|
114 | } |
---|
115 | } |
---|
116 | }; |
---|
117 | |
---|
118 | if (o.animation != 'always-on'){ |
---|
119 | //when the mouse is over the image |
---|
120 | $(this).hover( |
---|
121 | function() { |
---|
122 | over_img = true; |
---|
123 | if (!over_caption) { |
---|
124 | var props = o.animation == 'fade' |
---|
125 | ? { opacity: o.opacity } |
---|
126 | : { marginTop: captionPosition.show }; |
---|
127 | caption.animate(props, o.speedOver); |
---|
128 | if (o.animation == 'fade'){ |
---|
129 | captionContent.animate({opacity: 1}, o.speedOver/2); |
---|
130 | } |
---|
131 | } |
---|
132 | }, |
---|
133 | function() { |
---|
134 | over_img = false; |
---|
135 | window.setTimeout(cHide, o.hideDelay); |
---|
136 | } |
---|
137 | ); |
---|
138 | //when the mouse is over the caption on top of the image (the caption is a sibling of the image) |
---|
139 | $('div', wrapper).hover( |
---|
140 | function() { over_caption = true; }, |
---|
141 | function() { over_caption = false; window.setTimeout(cHide, o.hideDelay); } |
---|
142 | ); |
---|
143 | } |
---|
144 | }); |
---|
145 | //if the image has already loaded (due to being cached), force the load function to be called |
---|
146 | if (this.complete || this.naturalWidth > 0) { |
---|
147 | $(img).trigger('load'); |
---|
148 | } |
---|
149 | }); |
---|
150 | } |
---|
151 | }); |
---|