-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfittext.js
99 lines (89 loc) · 2.66 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*!
* FitText.js 1.0 version minus JQuery
*
* Copyright 2013, Dine Albrecht http://dinealbrecht.com
* Released under the MIT license
* https://github.com/dinealbrecht/fittext
*
* Date: 20th Jan 2014 20:00:54 GMT+0000
*/
(function(exports, _) {
'use strict';
/**
* Fittext
*
* Example Usage:
* var fittext = new Fittext(element, 0.2);
*
* @param object element - element to apply fittext
* @param number factor - resizing factor
* @param object options - options
* @return object - Fittext
*/
function Fittext(element, factor, options) {
this.element = element;
this.factor = factor;
this.active = true;
this.settings = _.extend({}, Fittext.defaults, options);
element.attributes['data-fittext'] = this;
if (this.settings.active)
this.activate();
}
/**
* resize
* should be called on window resize
*
* Example Usage:
* textResizer.resize();
*/
Fittext.prototype.getMinWidth = function() {
return this.element.getAttribute('data-minwidth');
};
/**
* resize
* should be called on window resize
*
* Example Usage:
* textResizer.resize();
*/
Fittext.prototype.doresize = function() {
this.element.style.fontSize = Math.max(Math.min(this.element.clientWidth / (this.factor*10), parseFloat(this.settings.maxFontSize)), parseFloat(this.settings.minFontSize)) + 'px';
};
/**
* conditionalresize
* should be called on window resize
*
* Example Usage:
* textResizer.conditionalresize();
*/
Fittext.prototype.conditionalresize = function() {
if (window.innerWidth > this.settings.minwidth) {
this.element.style.fontSize = Math.max(Math.min(this.element.clientWidth / (this.factor*10), parseFloat(this.settings.maxFontSize)), parseFloat(this.settings.minFontSize)) + 'px';
} else {
this.element.style.fontSize = '';
}
};
/**
* activate
* activate resize event listener
*
* Example Usage:
* fittextElement.activate();
*/
Fittext.prototype.activate = function() {
if ( this.settings.minwidth ) {
window.addEventListener('resize', this.conditionalresize.bind(this));
this.conditionalresize();
} else {
window.addEventListener('resize', this.doresize.bind(this));
this.doresize();
}
};
Fittext.defaults = {
'minFontSize' : -1/0,
'maxFontSize' : 1/0,
'active' : true,
'startSize' : 0
};
exports.Fittext = Fittext;
})(exports, exports._);