-
Notifications
You must be signed in to change notification settings - Fork 7
/
jquery.fit.js
50 lines (44 loc) · 1.2 KB
/
jquery.fit.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
/**
* (c) 2010, Andrew Gwozdziewycz, GPL Licensed, see LICENSE
*/
(function($) {
$.fn.fit = function(options) {
var config = {
'minsize': 1,
'maxsize': 10000,
'width': null,
'height': null
};
if (options) {
$.extend(config, options);
}
var fits = function(sz, node) {
node.css('fontSize', sz + 'px');
return node.outerWidth() <= config['width'] &&
node.outerHeight() <= config['height'];
};
this.each(function() {
var parent = $(this).parent();
if (!config['height']) {
config['height'] = parent.outerHeight();
}
if (!config['width']) {
config['width'] = parent.outerWidth();
}
var max = config['maxsize'];
var min = config['minsize'];
var iterations = 100;
do {
var hfs = max - min;
var mid = min + (hfs / 2);
if (fits(Math.floor(mid), $(this))) {
min = mid + 1;
}
else {
max = mid - 1;
}
} while (max > min && iterations--);
$(this).css('fontSize', fits(Math.floor(mid), $(this)) ? mid + 'px': min + 'px');
});
};
})(jQuery);