-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.textareaAutoSize.js
executable file
·75 lines (58 loc) · 2.34 KB
/
jquery.textareaAutoSize.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
/**
* Simple jQuery plugin auto resizes a textarea for content.
* @author Nabeel Javaid <[email protected]>
* @version 1.0
*
* @example $('textarea').textareaAutoSize({
* min_height: 30, // Default: 30
* max_height: 100 // Default: 100
* });
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lessier General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lessier General Public License for more details.
*
* You should have received a copy of the GNU Lessier General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
(function ( $ ) {
$.fn.textareaAutoSize = function( options ) {
// This is the easiest way to have default options.
var settings = $.extend({
// These are the defaults.
min_height: 30,
max_height: 100
}, options );
$elments=this;
return $elments.each(function() {
//Add event for each element
$(this, 'textarea').on('input keyup', function(event) {
if($(this).is('textarea'))
{
var $window = $(window);
var $current_scroll_position = $window.scrollTop();
//Apply Min Height Check
var $changed_height=this.scrollHeight;
if($changed_height<settings.min_height)
{
var $changed_height=settings.min_height;
}
if($changed_height>settings.max_height)
{
$(this).css('overflow-y', 'scroll');
$window.scrollTop($current_scroll_position);
}
else
{
$(this).css('overflow-y', 'hidden').css('height', $changed_height);
}
}
});
});//endeach
};
}( jQuery ));