-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathjquery.fittext.js
68 lines (60 loc) · 2.2 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
67
68
/*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($){
//jquery fitText([number compressor,] object options)
$.fn.fitText=function(compressor, options){
options=options || {};
if(compressor){
if(typeof compressor=="object"){
//If the options object was specified using the first arguments, the second argument is ignored.
options=compressor;
}else{
//If the compressor was specified using the first arguments, that value is used, regardless of the options object.
options.compressor=compressor;
}
}
//Setup options
var settings=$.extend({
//Number
"compressor":1,
//Must be parseable by `parseFloat()`
"minFontSize":Number.NEGATIVE_INFINITY,
//Must be parseable by `parseFloat()`
"maxFontSize":Number.POSITIVE_INFINITY,
//A function called before the new font-size is applied. The font-size
//is passed as first argument and `this` belongs to the DOM element.
//If `false` (checked using `===`) is returned, the font-size won't be applied.
"before":$.noop,
//A function called after the new font-size was applied. The font-size
//is passed as first argument and `this` belongs to the DOM element.
"after":$.noop,
//A jQuery object used instead of the object this function was called on.
//Does not affect `this` in "before" and "after".
"proxy":null
}, options);
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(){
var size=Math.max(Math.min($this.width()/(settings.compressor*10), parseFloat(settings.maxFontSize)), parseFloat(settings.minFontSize));
if(settings.before.apply($this.get(0), [size])!==false){
(settings.proxy || $this).css("font-size", size);
}
settings.after.apply($this.get(0), [size]);
};
//Call once to set.
resizer();
//Call on resize. Opera debounces their resize by default.
$(window).on("resize.fittext orientationchange.fittext", resizer);
});
};
})(jQuery);