-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfg.textarea.autosize.js
59 lines (48 loc) · 1.92 KB
/
fg.textarea.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
49
50
51
52
53
54
55
56
57
58
59
/**
* Prologue :
*
* Textarea autosize script
* By Francois Gelinas
* April 2012
*
* Require : jQuery 1.7 or earlier
*/
/* Chapter 1 : the setup */
(function( $, document ) {
$.fn.textareaAutosize = function() {
createMagicElement();
/* Chapter 2 : meet the characters */
this.each(function() {
$(this).on( 'keydown.textareaAutosize', textareaKeyDown )
.on( 'keyup.textareaAutosize', textareaKeyUp )
.css( 'overflow', 'hidden' ); // prevent scrollbars
adjustHeight.apply(this);
});
}
/* Chapter 3 : Some action */
var
textareaKeyUp = function() {
adjustHeight.apply(this);
},
textareaKeyDown = function() {
// adjustHeight.apply(this);
},
adjustHeight = function() {
var textarea = $(this), // why yes I like to prefix jQuery element variable with a $
content = textarea.val()
.replace( /\n/g, '<br>' )
+ '<br><br>'; // add extras <br> cause it feels more responsive this way
magicElement.css( 'font-size', textarea.css( 'font-size' ) )
.css( 'font-family', textarea.css( 'font-family') )
.html( content ) // replace carriage return with break rows
.css( 'width', textarea.width() );
textarea.css( 'height', magicElement.height());
},
/* Chapter 4 : the magic - our helper element */
magicElement = $('<div id="textareaAutosizeMagicHelper" style="display: inline-block; display: none"></div>'),
createMagicElement = function() {
if (magicElement.parent().typeName != 'body') {
magicElement.appendTo('body');
}
};
})( jQuery, document );