这是一个用jQuery实现的, 自动调整textarea高度, 非常的好!
但原作者已经把它的相关描述页面移除了, 这里做个备份吧~
但js路径还在:
full: http://james.padolsey.com/demos/plugins/jQuery/autoresize.jquery.js
minified: http://james.padolsey.com/demos/plugins/jQuery/autoresize.jquery.min.js
原文路径: http://james.padolsey.com/javascript/jquery-plugin-autoresize/ (现在是404)

Usage:
The following options are available:
onResize - (type: Function) - A callback function fired every time the textarea is resized. Within the function 'this' refers to the textarea being resized.
animate - (type: Boolean) - If set to false no animation will take place, the height will immediately change when necessary. By default it's set to true.
animateDuration - (type: Number) - Millisecond duration of animation, by default it's set to 150.
animateCallback - (type: Function) - A callback function fired every time an animation completes. Note: not the same as the onResize callback.
extraSpace - (type: Number) - A pixel value to be added to the total necessary height when applied to the textarea. By default it's set to 20. The idea behind this is to reassure users that they have more space to continue writing.
limit - (type: Number) - Once the textarea reaches this height it will stop expanding. By default it's set to 1000.

The textarea will expand when required until the limit is reached, at which time it brings back the scrollbar. If you were to then delete all the contents of the textarea it would only return to it's original size (no smaller). Also note that, even if you set 'animate' to true, the animation will only occur if the element is set to display:block; in the CSS.

$('textarea#comment').autoResize({
// On resize:
onResize : function() {
$(this).css({opacity:0.8});
},
// After resize:
animateCallback : function() {
$(this).css({opacity:1});
},
// Quite slow animation:
animateDuration : 300,
// More extra space:
extraSpace : 40
});
/*
* jQuery autoResize (textarea auto-resizer)
* @copyright James Padolsey http://james.padolsey.com
* @version 1.04
*/ (function($){ $.fn.autoResize = function(options) { // Just some abstracted details,
// to make plugin users happy:
var settings = $.extend({
onResize : function(){},
animate : true,
animateDuration : 150,
animateCallback : function(){},
extraSpace : 20,
limit: 1000
}, options); // Only textarea's auto-resize:
this.filter('textarea').each(function(){ // Get rid of scrollbars and disable WebKit resizing:
var textarea = $(this).css({resize:'none','overflow-y':'hidden'}), // Cache original height, for use later:
origHeight = textarea.height(), // Need clone of textarea, hidden off screen:
clone = (function(){ // Properties which may effect space taken up by chracters:
var props = ['height','width','lineHeight','textDecoration','letterSpacing'],
propOb = {}; // Create object of styles to apply:
$.each(props, function(i, prop){
propOb[prop] = textarea.css(prop);
}); // Clone the actual textarea removing unique properties
// and insert before original textarea:
return textarea.clone().removeAttr('id').removeAttr('name').css({
position: 'absolute',
top: 0,
left: -9999
}).css(propOb).attr('tabIndex','-1').insertBefore(textarea); })(),
lastScrollTop = null,
updateSize = function() { // Prepare the clone:
clone.height(0).val($(this).val()).scrollTop(10000); // Find the height of text:
var scrollTop = Math.max(clone.scrollTop(), origHeight) + settings.extraSpace,
toChange = $(this).add(clone); // Don't do anything if scrollTip hasen't changed:
if (lastScrollTop === scrollTop) { return; }
lastScrollTop = scrollTop; // Check for limit:
if ( scrollTop >= settings.limit ) {
$(this).css('overflow-y','');
return;
}
// Fire off callback:
settings.onResize.call(this); // Either animate or directly apply height:
settings.animate && textarea.css('display') === 'block' ?
toChange.stop().animate({height:scrollTop}, settings.animateDuration, settings.animateCallback)
: toChange.height(scrollTop);
}; // Bind namespaced handlers to appropriate events:
textarea
.unbind('.dynSiz')
.bind('keyup.dynSiz', updateSize)
.bind('keydown.dynSiz', updateSize)
.bind('change.dynSiz', updateSize); }); // Chain:
return this; }; })(jQuery);

此插件在行数少时, 比如一两行, 回车时, 第一行会有抖动的现象, 下边的有所改善:
我发现了一个更好的: http://www.jacklmoore.com/autosize/

jQuery autoResize的更多相关文章

  1. myWaterfall - jQuery瀑布流布局插件

    myWaterfall - jQuery瀑布流布局插件 Demo http://jsfiddle.net/q3011893/p5k2ogy8/embedded/result,html,css,js/ ...

  2. jQuery 滚动条 滚动到底部(下拉到底部) 加载数据(触发事件、处理逻辑)、分页加载数据

    1.针对浏览器整个窗口滚动 主要代码: <script type="text/javascript"> ; function GetProductListPageFun ...

  3. jQuery Wookmark Load 瀑布流布局实例演示

    瀑布流布局非常适合大量图片的展示,一改过去裁剪图片尺寸统一的排版,每张图片都能完全展示,并错落有致,让人眼前一亮. 版本: jQuery v1.4.3+ jQuery Wookmark Load v1 ...

  4. jQuery插件之simplemodal

    1.simplemodal在内部定义了如下css类 simplemodal-overlay:遮罩 simplemodal-container:弹出窗口容器 simplemodal-wrap simpl ...

  5. 使用Jquery+EasyUI 进行框架项目开发案例讲解之四 组织机构管理源码分享

    http://www.cnblogs.com/huyong/p/3404647.html 在上三篇文章  <使用Jquery+EasyUI进行框架项目开发案例讲解之一---员工管理源码分享> ...

  6. jQuery插件之Wookmark瀑布流

    使用方法: 1.下载wookmark.js 2.构建html <div class="wrapper"> <div id="con1_1"&g ...

  7. jQuery Wookmark 瀑布流布局

    瀑布流布局非常适合大量图片的展示,一改过去裁剪图片尺寸统一的排版,每张图片都能完全展示,并错落有致,让人眼前一亮. 版本: jQuery v1.4.3+ jQuery Wookmark Load v1 ...

  8. jquery nicescroll 配置参数

    jQuery滚动条插件兼容ie6+.手机.ipad http://www.areaaperta.com/nicescroll/ jQuery(function($){ $("#scrollI ...

  9. 自写图片遮罩层放大功能jquery插件源代码,photobox.js 1.0版,不兼容IE6

    阿嚏~~~ 话说本屌丝没啥开发插件的经验,可是天公不作美,公司须要让我自己开发个图片放大的插件 但公司老大的话,宛如吾皇之圣旨,微臣必当肝脑涂地,莫敢不从啊~~~ 于是乎,作为一个超级小白,本人仅仅能 ...

随机推荐

  1. Data 语义学(1)

    一.Data Member 的绑定(The binding of Data Member) extern float x; class Point3d { public: Point3d( float ...

  2. -_-#【jQuery插件】textSlider 文本滚动

    jQuery.textSlider.js ;(function($) { $.fn.textSlider = function(settings) { settings = jQuery.extend ...

  3. 【最短路】BAPC2014 B Button Bashing (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  4. C#中object sender,EventHandler e有个毛作用

    button1_Click(object sender,EventHandler e) { Button button=(Button)sender; button.Text="text p ...

  5. ACM编程网站

    ACM:ACM国际大学生程序设计竞赛(英文全称:ACM International Collegiate ProgrammingContest(ACM-ICPC或ICPC)是由美国计算机协会(ACM) ...

  6. apache POI 导出excel相关方法

    apache POI 操作excel无比强大.同时有操作word和ppt的接口. 下面讲解poi中常用方法. 1,设置列宽 HSSFSheet sheet = wb.getSheetAt(0); sh ...

  7. MySQL ERROR 1045错误解决办法

    今天在安装MySQL数据库时,有安装过程中报ERROR 1045错误,网上查了一下,解决方法如下: 1.对于安装过程中该错选择"skip"继续向下安装,完成安装: 2.在MySQL ...

  8. php数组和字符串转换

    PHP 中由于数组和字符串这两种变量类型是如此常用,以至于 PHP 具有两个函数,可以在字符串和数组之间互相进行转换. $array=explode(separator,$string); $stri ...

  9. 项目经历——EasyUI的检索和更新操作

    什么事都耐不住“磨”?! 比如一个让人很头疼的问题,对于项目中所用到的EasyUI框架就是一个很明显的感受.项目使用的EasyUI框架,自身封装好的样式使得开发者不用考虑具体的div和css样式了,但 ...

  10. 利用MyEclipse配置S2SH三大框架篇-Spring配置

    1.配置完Struts2后,然后配置Spring 2.单击“MyEclipse->Project Capabilities->Add Spring Capabilities” 3.选择Sp ...