diff options
author | Karl Swedberg <karl@englishrules.com> | 2009-04-30 13:16:51 -0400 |
---|---|---|
committer | Karl Swedberg <karl@englishrules.com> | 2009-04-30 13:16:51 -0400 |
commit | 97cfc7b707d97b0d479fc5fbb7ea79cda3cb25a2 (patch) | |
tree | 90ce9cb03e1b4fa2164156b49ae758617d4c93d9 | |
parent | 77b5be09ef4cf5c0f07b7747e5f955de7ec774c4 (diff) | |
download | jquery-expandable-97cfc7b707d97b0d479fc5fbb7ea79cda3cb25a2.zip jquery-expandable-97cfc7b707d97b0d479fc5fbb7ea79cda3cb25a2.tar.gz jquery-expandable-97cfc7b707d97b0d479fc5fbb7ea79cda3cb25a2.tar.bz2 |
added init option to set textarea height initially on load. fixed set/clear Interval.
-rw-r--r-- | jquery.expandable.js | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/jquery.expandable.js b/jquery.expandable.js index 525e44d..60fb0b9 100644 --- a/jquery.expandable.js +++ b/jquery.expandable.js @@ -7,7 +7,7 @@ $.fn.extend({ expandable: function(options) { - options = $.extend({ duration: 'normal', interval: 750, within: 1, by: 2 }, options); + options = $.extend({ duration: 'normal', interval: 750, within: 1, by: 2, init: false }, options); return this.filter('textarea').each(function() { var $this = $(this).css({ display: 'block', overflow: 'hidden' }), minHeight = $this.height(), interval, heightDiff = this.offsetHeight - minHeight, rowSize = ( parseInt($this.css('lineHeight'), 10) || parseInt($this.css('fontSize'), 10) ), @@ -19,8 +19,9 @@ $.fn.extend({ .bind('keypress', function(event) { if ( event.keyCode == '13' ) check(); }) .bind('focus blur', function(event) { if ( event.type == 'blur' ) clearInterval( interval ); - if ( event.type == 'focus' && !interval ) setInterval(check, options.interval); + if ( event.type == 'focus' ) interval = setInterval(check, options.interval); }); + function check() { var text = $this.val(), newHeight, height, usedHeight, usedRows, availableRows; $div.html( text.replace(/\n/g, ' <br>') ); @@ -32,10 +33,11 @@ $.fn.extend({ newHeight = rowSize * (usedRows + Math.max(availableRows, 0) + options.by); $this.stop().animate({ height: newHeight }, options.duration); } else if ( availableRows > options.by + options.within ) { - newHeight = Math.max( height - (rowSize * (availableRows - (options.by + options.within))), minHeight ) + newHeight = Math.max( height - (rowSize * (availableRows - (options.by + options.within))), minHeight ); $this.stop().animate({ height: newHeight }, options.duration); } }; + if ( options.init ) check(); }).end(); } }); |