这是一个用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. open和fopen的区别(转)

    转载自:http://www.cnblogs.com/joeblackzqq/archive/2011/04/11/2013010.html open和fopen的区别: 1.缓冲文件系统缓 冲文件系 ...

  2. 吉哥系列故事――临时工计划(dp)

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Descr ...

  3. matlab制造一个64*64的仿真数据

    fid = fopen('test_001.img','w'); r=random('Normal',100,0,64,64); z=random('Uniform',0,5,64,64); %%%% ...

  4. (转载)php 合并数组中的数据,如果键值相等其值相加

    (转载)http://hi.baidu.com/syxrrrr/item/7dfb2387343ce0874414cfdd /* * 合并数据,如果键值相等其值相加 * @param array $d ...

  5. 暴力求解——除法 Division,UVa 725

    Description Write a program that finds and displays all pairs of 5-digit numbers that between them u ...

  6. [LeetCode] 141&142 Linked List Cycle I & II

    Problem: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without ...

  7. How To Create a New User and Grant Permissions in MySQL

    How to Create a New User Let’s start by making a new user within the MySQL shell: CREATE USER 'newus ...

  8. motan源码分析十:流量切换

    motan提供了流量切换的功能,可以实现把一个group的流量切换到另一个group(一个或多个服务都可以).大家可以使用tomcat部署motan的管理工具,并设置几个组,例如可以参考demo代码: ...

  9. J2EE之WebLogic Server

    WebLogic是用于开发.集成.部署和管理大型分布式Web应用. 网络应用和数据库应 用的Java应用server. 将Java的动态功能和Java Enterprise标准的安全性引入大型网络应用 ...

  10. String.Split()函数

    我们在上次学习到了 String.Join函数(http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx),当中用到了String.SPl ...