<!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="http://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>
jquery 控制textArea 随文本增加而变高
最新推荐文章于 2024-04-18 14:46:56 发布