forked from davatron5000/FitText.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.fittext.js
More file actions
59 lines (52 loc) · 1.46 KB
/
jquery.fittext.js
File metadata and controls
59 lines (52 loc) · 1.46 KB
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
/*global jQuery */
/*!
* FitText.js 1.2
*
* Copyright 2011, Dave Rupert http://daverupert.com
* Released under the WTFPL license
* http://sam.zoy.org/wtfpl/
*
* Date: Thu May 05 14:23:00 2011 -0600
*/
(function ($) {
$.fn.fitText = function (kompressor, options) {
// Setup options
var compressor = kompressor || 1,
settings = $.extend(
{
minFontSize: Number.NEGATIVE_INFINITY,
maxFontSize: Number.POSITIVE_INFINITY,
},
options
);
var pxToRem = function ptr(px) {
var x = $(window).width() / 90,
rem = (3 / x) * px;
return rem;
};
var remToPx = function convertRemToPixels(rem) {
return rem * parseFloat(getComputedStyle(document.documentElement).fontSize);
};
return this.each(function () {
// Store the object
var $this = $(this);
// Resizer() resizes items based on the object width divided by the compressor * 10
var resizer = function () {
$this.css(
"font-size",
pxToRem(Math.max(
Math.min(
$this.width() / (compressor * 10),
parseFloat(settings.maxFontSize)
),
parseFloat(settings.minFontSize)
)) + 'rem'
);
};
// Call once to set.
resizer();
// Call on resize. Opera debounces their resize by default.
//$(window).on("resize.fittext orientationchange.fittext", resizer);
});
};
})(jQuery);