-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.autoresize.js
117 lines (75 loc) · 2.91 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
autoresize 1.0 - jQuery plugin
written by O! Interactive, Acuna
http://ointeractive.ru
Copyright (c) 2016 O! Interactive, Acuna (http://ointeractive.ru)
Dual licensed under the MIT (MIT-LICENSE.txt)
and GPL (GPL-LICENSE.txt) licenses.
Built for jQuery library
http://jquery.com
1.0 13.06.2016
Первый приватный релиз
*/
(function ($) {
$.fn.autoResize = function (options) {
var options = $.extend ({
'onResize': function (){},
'animate': true,
'animateDuration': 150,
'animateCallback': function (){},
'extraSpace': 0,
'limit': 500,
'minHeight': 50,
'autoHeight': true,
}, options);
$(this).filter ('textarea').each (function () {
var textarea = $(this).css ({ 'resize':'none', 'overflow-y':'hidden' }),
origHeight = textarea.height (),
clone = (function () {
var
props = ['height', 'width', 'lineHeight', 'textDecoration', 'letterSpacing'],
propOb = {};
$.each (props, function (i, prop){
propOb[prop] = textarea.css (prop);
});
return textarea.clone ().removeAttr ('id').removeAttr ('name').css ({
'position': 'absolute',
'top': 0,
'left': -9999
}).css (propOb)
.attr ('tabIndex', '-1')
.insertBefore (textarea)
.addClass ('auto-resize');
})(),
lastScrollTop,
updateSize = function () {
clone.height (0).val ($ (this).val ()).scrollTop (10000);
var
scrollTop = Math.max (clone.scrollTop (), origHeight) + options.extraSpace,
toChange = $ (this).add (clone);
if (lastScrollTop !== scrollTop) {
lastScrollTop = scrollTop;
if (scrollTop >= options.limit && options.limit != 0) {
$ (this).css ({ 'overflow-y':'', 'height':options.limit+'px' });
return;
}
}
options['onResize'] (this, origHeight, options);
options.animate && textarea.css ('display') === 'block' ?
toChange.stop ().animate ({height:scrollTop}, options.animateDuration, options.animateCallback) :
toChange.height (scrollTop);
};
var value = textarea.val ();
if (options.autoHeight && value > textarea.height) {
var height = (value.length / 2);
if (height < options.minHeight) height = options.minHeight;
textarea.css ({ 'height':height + 'px', 'overflow-y':'auto' });
}
textarea.unbind ('.dynSiz')
.on ('keyup.dynSiz', updateSize)
.on ('keydown.dynSiz', updateSize)
.on ('change.dynSiz', updateSize);
});
return this;
};
})(jQuery);