forked from ryun/HCaptions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.hcaptions.js
241 lines (198 loc) · 4.98 KB
/
jquery.hcaptions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
(function($){
var Captions = function(el, opts) {
var _this = this,
$this = $(el),
$el = $this.clone(),
href = $this.attr('href'),
$target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))), //strip for ie7
_overlay_css = {};
if ( ! $target.length )
{
$target = $this.next(opts.data_selector);
}
if ($target.length) {
this.set_from_attr(el, opts);
$wrap = $('<div class="drop-panel" />',{position: 'relative', 'z-index': 1, display: 'block', overflow: 'hidden'})
.append($el)
.append($target);
$this.replaceWith($wrap);
$target.hide();
$wrap.css({ 'position':'relative', 'overflow':'hidden', display: 'block', padding:'2px' });
if (opts.find_image && $this.not('img'))
{
var img = $wrap.find('img'),
w = img.width(),
h = img.height();
}
else {
var w = $wrap.outerWidth(),
h = $wrap.outerHeight();
}
var overlay_w = opts.width || w,
overlay_h = opts.height || h;
$target.css({ 'width':overlay_w, 'height':overlay_h, 'position':'absolute', 'z-index':33, overflow: 'hidden' });
var _overlay_css = {};
if (opts.overlay_bg) {
_overlay_css.background = opts.overlay_bg;
}
if (opts.overlay_opacity<1) {
_overlay_css.opacity = opts.overlay_opacity;
}
// CSS: Overlay X Position
_overlay_css.left = (opts.overlay_x == 'left')
? 0
: (opts.overlay_x == 'right')
? w-overlay_w
: (w - overlay_w) / 2 + 'px';
// CSS: Overlay Y Position
_overlay_css.top = (opts.overlay_y == 'top')
? 0
: (opts.overlay_y == 'bottom')
? h-overlay_h
: (h - overlay_h) / 2 + 'px';
// CSS: Apply rules
$target.css(_overlay_css);
// slide effect
if (opts.effect=='slide') {
var slide_css = {};
switch (opts.direction) {
case 'top':
slide_css.top = '-'+overlay_h+'px';
break;
case 'bottom':
slide_css.top = h+'px';
break;
case 'left':
slide_css.left = '-'+overlay_w+'px';
break;
case 'right':
default:
slide_css.left = w+'px';
break;
}
// Apply Slide rules
$target.css('z-index',opts.zindex+1).css(slide_css);
// Hover events
$wrap.hover(function(){
$target.show().stop(true, true).animate({ 'top': _overlay_css.top, 'left': _overlay_css.left }, +opts.speed, opts.onshow());
}, function(){
$target.show().stop(true, true).animate(slide_css, +opts.speed, opts.onhide());
});
// fade effect
} else if (opts.effect=='fade') {
$target.css('z-index',opts.zindex+1).hide();
$wrap.hover(function () {
$target.stop(true, true).fadeIn(+opts.speed, opts.onshow());
}, function () {
$target.stop(true, true).fadeOut(+opts.speed, opts.onhide());
});
// just show/hide
} else {
$target.css('z-index',opts.zindex+1).hide();
$wrap.hover(function () {
$target.show(0, opts.onshow());
}, function () {
$target.hide(0, opts.onhide());
});
}
}
};
Captions.prototype = {
constructor: Captions,
set_from_attr: function(el, opt){
var cfg={},
attrs=el.attributes,
l=attrs.length;
for (var i=0; i<l; i++)
{
attr = attrs.item(i);
if (/cap-/i.test(attr.nodeName))
{
opt[attr.nodeName.replace('cap-', '')] = attr.nodeValue;
}
}
}
};
$.fn.hcaptions = function (option) {
return this.each(function () {
var $this = $(this)
, data = $this.data('captions')
, options = $.extend({}, $.fn.hcaptions.defaults, $this.data(), typeof option == 'object' && option);
if (!data) $this.data('captions', (data = new Captions(this, options)));
if (typeof option == 'string') data[option]();
});
};
$.fn.hcaptions.defaults = {
/**
* Selector for caption content
* @type {String}
*/
data_selector: '.cap-overlay',
/**
* Overlay width
* @default full width
* @type {Number}
*/
width: 0,
/**
* Overlay height
* @type {Number}
*/
height: 0,
/**
* Horizontal position for the overlay
* @options [center, left, right]
* @type {String}
*/
overlay_x: 'center',
/**
* Vertical position for the overlay
* @options [center, top, bottom]
* @type {String}
*/
overlay_y: 'center',
/**
* Background css for overlay
* @type {String}
*/
overlay_bg: '',
/**
* Opacity of overlay
* @type {Number}
*/
overlay_opacity: 1,
/**
* Effect of overlay
* @options [fade, slide, show/hide]
* @type {String}
*/
effect: 'slide',
/**
* Animation speed in ms
* @type {Number}
*/
speed: 400,
/**
* Direction of overlay
* @options [top, bottom, right, left]
* @type {String}
*/
direction: 'top',
/**
* Z-Index Base
* @type {Number}
*/
zindex: 2,
find_image: false,
/**
* On show callback
* @return {[type]} [description]
*/
onshow: function(){},
/**
* On hide callback
* @return {[type]} [description]
*/
onhide: function(){}
};
})(jQuery);