-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.size-up.js
39 lines (28 loc) · 962 Bytes
/
jquery.size-up.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
/**
* Makes your headings fit its container perfectly.
* @author Joël Cox
*/
(function($) {
$.fn.sizeUp = function(options) {
var defaults = {
marginCorrection: 0,
};
var options = $.extend(defaults, options);
return this.each(function() {
// Wrapper the text in spans to we can check for the width
$(this).wrapInner('<span />');
var fontSize = parseInt($(this).css("font-size")),
currentElementWidth = $('span', this).width(),
previousElementWidth = 0,
elementWidthIncrease = 0,
maxElementWidth = $(this).parent().width();
while(currentElementWidth + elementWidthIncrease - options.marginCorrection < maxElementWidth){
$(this).css("font-size", fontSize);
previousElementWidth = currentElementWidth;
currentElementWidth = $('span', this).width();
elementWidthIncrease = currentElementWidth - previousElementWidth;
fontSize++;
}
});
};
})(jQuery);