-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfittext.js
60 lines (48 loc) · 1.53 KB
/
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
(function ( $ ) {
$.fn.fittext = function(options){
var opts = $.extend( {}, $.fn.fittext.defaults, options );
$.fn.fittext.defaults = {
storeFont: "no"
};
$(this).each(function(){
var divWidth, textWidth, childSpan;
divWidth = $(this).width();
childSpan = $(this).find('span:first');
textWidth = childSpan.width();
resizeFont(textWidth,divWidth,childSpan);
function resizeFont(textWidth,divWidth,childSpan){
if (textWidth>divWidth && parseInt(childSpan.css('font-size'))>0){
decreaseFont(divWidth,childSpan);
}else if (textWidth<divWidth && parseInt(childSpan.css('font-size'))<38){
increaseFont(divWidth,childSpan);
}
};
function increaseFont(divWidth,childSpan){
var newFontSize;
while (parseInt(childSpan.width())<divWidth){
newFontSize = parseInt(childSpan.css('font-size'))+1;
childSpan.css({ 'font-size': newFontSize});
if (parseInt(childSpan.width())>divWidth || parseInt(childSpan.css('font-size'))>38){
decreaseFont(divWidth,childSpan);
return;
}
}
storeFont(newFontSize);
};
function decreaseFont(divWidth,childSpan){
var newFontSize;
while (parseInt(childSpan.width())>divWidth){
newFontSize = parseInt(childSpan.css('font-size'))-1;
childSpan.css({ 'font-size': newFontSize });
}
storeFont(newFontSize);
};
function storeFont(newFontSize){
if (opts.storeFont === "yes"){
localStorage.setItem("fontSize", newFontSize);
}
};
});
return $(this);
};
}( jQuery ));