-
Notifications
You must be signed in to change notification settings - Fork 9
/
jquery.autoresize.js
98 lines (77 loc) · 2.75 KB
/
jquery.autoresize.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
/*!
* jQuery textarea autoResize plugin v0.2.1
* http://github.com/thomasjo/jquery-autoresize
*
* Copyright (c) 2011 Thomas Johansen | https://raw.github.com/thomasjo/jquery-autoresize/master/LICENSE.txt
*/
;(function() {
'use strict';
var root = this,
$ = root.$,
defaults = {
resize: $.noop
};
$.fn.autoResize = function(options) {
var settings = $.extend({}, defaults, options);
this.filter('textarea').each(function() {
var $textarea = $(this).css({
'overflow-y': 'hidden',
'resize': 'none'
}),
minHeight = $textarea.height(),
previousHeight = minHeight,
$slave = (function() {
var $clone = $textarea.clone()
.attr('tabindex', -1)
.removeAttr('id')
.removeAttr('name')
.css({
'position': 'absolute',
'top': 0,
'left': -9999
});
return $clone.insertBefore($textarea);
})(),
adjustHeightIfNeeded = function () {
var text = $textarea.val(),
height;
$slave
.width($textarea.width())
.height(0)
.val(text)
.scrollTop(9999);
height = Math.max(minHeight, $slave.scrollTop());
if (height === previousHeight) {
return;
}
previousHeight = height;
settings.resize.call(this);
$textarea.height(height);
};
$textarea.unbind('.resize');
if (supportsInputEvent()) {
$textarea.bind('input.resize', adjustHeightIfNeeded);
}
else if (supportsPropertyChangeEvent()) {
$textarea.bind('propertychange.resize', adjustHeightIfNeeded);
}
else {
$textarea.bind('keypress.resize', adjustHeightIfNeeded);
}
adjustHeightIfNeeded();
});
return this;
};
function supportsInputEvent() {
if ('oninput' in document.body) {
return true;
}
document.body.setAttribute('oninput', 'return');
var supports = typeof document.body.oninput === 'function';
delete document.body.oninput;
return supports;
}
function supportsPropertyChangeEvent() {
return 'onpropertychange' in document.body;
}
}).call(this);