-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautosize.js
48 lines (39 loc) · 1.15 KB
/
autosize.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
function autoSizeEvt(e) {
autoSize(e.target);
}
function windowResizeEvt(e) {
resizeAllTextareas();
}
function resizeAllTextareas() {
var taArray = getTextareasArray();
taArray.forEach(function (ta) {
autoSize(ta);
});
}
function getTextareasArray() {
var taArray = [];
var taList = document.getElementsByTagName('textarea');
for (var i = 0; i < taList.length; i++) {
taArray.push(taList[i]);
}
return taArray;
}
function autoSize(ta) {
var style = window.getComputedStyle(ta, null);
ta.style.height = 'auto';
ta.style.overflowY = 'hidden';
var scrollHeight = ta.scrollHeight;
var offsetHeight;
if (style.boxSizing === 'content-box') {
offsetHeight = -(parseFloat(style.paddingTop) + parseFloat(style.paddingBottom));
} else {
offsetHeight = parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth);
}
ta.style.height = scrollHeight + offsetHeight + 'px';
}
var taArray = getTextareasArray();
taArray.forEach(function (ta) {
autoSize(ta);
ta.addEventListener('input', autoSizeEvt);
});
window.addEventListener('resize', windowResizeEvt);