forked from davatron5000/FitText.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.fittext.js
66 lines (42 loc) · 1.72 KB
/
jquery.fittext.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
(function( $ ){
$.fn.fitText = function( options ) {
// Setup options
var settings = $.extend({
'minFontSize' : Number.NEGATIVE_INFINITY,
'maxFontSize' : Number.POSITIVE_INFINITY
}, options);
return this.each(function(){
var $this = $(this);
var width = $this.wrapInner("<span id='fit' style='display:inline-block; white-space:nowrap;'></span>").find("#fit").width();
$this.html( $this.find("#fit").html() );
var fontSize = parseInt($this.css("font-size"), 10);
var init = true;
// Resizer() resizes items based on the object width divided by the compressor * 10
var resizer = function resizer() {
var multiplier = width/$this.width();
var newSize = fontSize / multiplier ;
// console.log(multiplier);
$this.addClass("justify");
if (newSize <= settings.minFontSize || newSize >= settings.maxFontSize) {
$this.removeClass("justify");
}
$.when(
$this.css('font-size', Math.max(Math.min(newSize, parseFloat(settings.maxFontSize)), parseFloat(settings.minFontSize)))
).done( function() {
if (init) {
width = $this.wrapInner("<span id='fit' style='display:inline-block; white-space:nowrap;'></span>").find("#fit").width();
$this.html( $this.find("#fit").html() );
fontSize = parseInt($this.css("font-size"), 10);
multiplier = width/$this.width();
newSize = fontSize / multiplier ;
$this.css('font-size', Math.max(Math.min(newSize, parseFloat(settings.maxFontSize)), parseFloat(settings.minFontSize)));
init = false;
}
});
};
resizer();
// Call on resize. Opera debounces their resize by default.
$(window).on('resize.fittext orientationchange.fittext', resizer);
});
};
})( jQuery );