-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.caption.js
90 lines (72 loc) · 2.14 KB
/
jquery.caption.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
/*
* jQuery.caption v0.9.1
*
*/
(function($){
var settings = {};
var methods = {
init : function(options){
settings = $.extend({
'figureClass' : 'figure',
'figcaptionClass' : 'figcaption',
'lineBreak' : ' - ',
'visible' : false
}, options);
return this.each(function(){
var alt = $(this).attr('alt');
if(alt !== undefined){
if(!$(this).data('caption')){
var html = "";
var lines = alt.split(new RegExp(settings.lineBreak));
for(var i = 0;i < lines.length;i++){
if(i === 0){
html += lines[i];
}
else{
html += "<br/>"+lines[i];
}
}
var figure = $('<figure />', {'class' : settings.figureClass});
var figcaption = $('<figcaption />', {html : html, 'class' : settings.figcaptionClass});
$(this).data('caption', {target : $(this), figure : figure, figcaption : figcaption});
}
$(this).wrap($(this).data('caption').figure);
$(this).after($(this).data('caption').figcaption);
if(settings.visible !== true){
$(this).data('caption').figcaption.hide();
}
$(this).bind('mouseenter.caption mouseleave.caption', function(){
if ($(this).data('caption').figcaption.css('display') == 'none') {
$(this).data('caption').figcaption.show();
}
else {
$(this).data('caption').figcaption.hide();
}
});
}
else{
$.error("'alt' attribute is not present on element");
}
});
},
destroy : function(){
return this.each(function(){
$(window).unbind('.caption');
$(this).data('caption').figure.remove();
$(this).data('caption').figcaption.remove();
$(this).removeData('caption');
});
}
};
$.fn.caption = function(method){
if(typeof method === 'object' || !method){
return methods.init.apply( this, arguments );
}
else if(methods[method]){
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
}
else{
$.error( 'Method ' + method + ' does not exist on jQuery.caption' );
}
};
})(jQuery);