-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathjquery.hover_caption.js
84 lines (69 loc) · 2.25 KB
/
jquery.hover_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
/**
* Hover Caption - jQuery plugin to add a simple hover effect
* and caption to images.
*
* Source Code: https://github.com/coryschires/hover-caption
*
* Copyright (c) 2011 Cory Schires (coryschires.com)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Version: 0.2.0
*/
(function($) {
$.hover_caption = {
defaults: {
caption_font_size: '18px',
caption_color: 'white',
caption_bold: true,
caption_default: "Click for screenshots."
}
}
$.fn.extend({
hover_caption: function(config) {
var config = $.extend({}, $.hover_caption.defaults, config);
return this.each(function() {
var image = $(this);
// set variable for wrapper div
var width = image.width();
var height = image.height();
// variables for caption
var caption_padding = width * .07; // dynamic margin depending on img width
// set caption to title attr if set
var caption = image.attr('title') ? image.attr('title') : config.caption_default;
// add necessary html and css
image
.css({
'z-index': '-1',
'position': 'relative'
})
.wrap('<div>')
.parent()
.css({
'width': width,
'height': height
})
.prepend('<h3>'+ caption +'</h3>')
.find('h3')
.addClass('hover_caption_caption') // use this hook for additional styling
.css({
'padding': caption_padding,
'color': config.caption_color,
'width': width,
'font-size': config.caption_font_size,
'position': 'absolute',
'margin': 0
})
.hide();
if (config.caption_bold) { image.css('font-weight', 'bold') };
// add hover event to toggle message
image.parent().hover(function() {
$(this).addClass('hover_caption').find('h3').show();
}, function() {
$(this).removeClass('hover_caption').find('h3').hide();
});
})
}
})
})(jQuery);