阅读背景:

jquery 控制textArea 随文本增加而变高

来源:互联网 
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>jquery 控制textArea 随文本增加而变高</title>
	<style type="text/css" media="screen">
		*{margin:0; padding: 0;}
		.f-wrap{
			width: 1000px;
			margin:30px auto;
		}
		textarea{
			text-indent: 2em;
			border:1px solid #ddd;
			width: 100%;
			height:150px;
			resize:none;
		}
	</style>
</head>
<body>
<div class="f-wrap">
	<textarea row="3" col="4" id="textarea"></textarea>
</div>
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script> 
 	$.fn.autoTextarea = function (options){
 		var defaults = {
 			maxHeight:null,
 			minHeight:$(this).height()
 		};
 		var opts = $.extend({}, defaults, options);

 		return $(this).each(function (){
 			var _this = $(this);
 			_this.bind('paste cur keydown keyup focus blur', function (){
 				var height, style = this.style;
 				style.height = opts.minHeight + 'px';
 				if(this.scrollHeight > opts.minHeight){
 					if(opts.maxHeight && this.scrollHeight > opts.maxHeight){
 						height = opts.maxHeight;
 						style.overflowY = 'scroll';
 					}else{
 						height = this.scrollHeight;
 						style.overflowY = 'hidden';
 					}
 					style.height = height + 'px';
 				}
 			});
 		});
 	}
    $(function (){
    	$('#textarea').autoTextarea({
    		maxHeight:200
    	});
    });
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: