Created
November 22, 2011 10:21
-
-
Save numbcoder/1385367 to your computer and use it in GitHub Desktop.
Textarea auto resize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.textResizer = function(options) { | |
var el = $(this), h; | |
var settings = { | |
minHeight: el.height(), | |
maxHeight: 300, | |
duration: 100 | |
} | |
if (options) { | |
$.extend(settings, options); | |
} | |
var clone = el.clone().removeAttr('id').removeAttr('class').css({position:'absolute', top:'-9999em',left:'-9999em',width: el.width(), height: 'auto'}).appendTo('body'); | |
$(this).keyup(function(e) { | |
h = clone.val(el.val()).height(0).scrollTop(10000).scrollTop() + 16; | |
h = Math.min(Math.max(settings.minHeight, h), settings.maxHeight); | |
if (el.height() != h) { | |
if($.browser.msie) { | |
el.height(h); | |
} else { | |
el.stop(1,1).animate({height: h}, settings.duration); | |
} | |
} | |
}); | |
return this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment