有时候写表单的时候,会有一个 备注框textarea。
因为textarea不支持自适应高度,就是定好高度或者是行数之后,超出部分就会显示滚动条,看起来不美观。
我们需要美观实现的效果:默认显示一行。当输入的文字超过一行或者输入Enter时,
输入框的高度会随着改变,直到输入完毕。也就是要实现textarea的高度自适应
=========================================================
方案A:用div来模拟textarea实现的,用CSS控制样式,不用JS。
而用DIV来模拟时,首先遇到的问题是:div怎么实现输入功能?
一个普通的block元素上加个contenteditable="true"就实现编辑,出现光标了。
如<div contenteditable="true"></div>
<div class="test-textarea" contenteditable="true" ><br /></div>
.test-textarea {
width: 400px;
min-height: 26px;
line-height: 20px;
_height: 30px;
/* max-height: 150px;*/
margin-left: auto;
margin-right: auto;
padding: 3px;
outline: 0;
border: 1px solid #ccc;
font-size: 12px;
word-wrap: break-word;
overflow-x: hidden;
overflow-y: auto;
-webkit-user-modify: read-write-plaintext-only;
border-radius: 4px;
}
===================================================
方案B:Js实现textarea自适应
由于第一种方法,改变了原有项目里面的结构标签
textarea需要改变为div
则,在原有基础上进行js修改来达到输入框自适应
<textarea id="textarea" placeholder="回复内容"></textarea>
<script type="text/javascript">
var autoTextarea = function(elem, extra, maxHeight) {
extra = extra || 0;
var isFirefox = !!document.getBoxObjectFor || ‘mozInnerScreenX‘ in window,
isOpera = !!window.opera && !!window.opera.toString().indexOf(‘Opera‘),
addEvent = function(type, callback) {
elem.addEventListener ?
elem.addEventListener(type, callback, false) :
elem.attachEvent(‘on‘ + type, callback);
},
getStyle = elem.currentStyle ? function(name) {
var val = elem.currentStyle[name];
if(name === ‘height‘ && val.search(/px/i) !== 1) {
var rect = elem.getBoundingClientRect();
return rect.bottom - rect.top -
parseFloat(getStyle(‘paddingTop‘)) -
parseFloat(getStyle(‘paddingBottom‘)) + ‘px‘;
};
return val;
} : function(name) {
return getComputedStyle(elem, null)[name];
},
minHeight = parseFloat(getStyle(‘height‘));
elem.style.resize = ‘none‘;
var change = function() {
var scrollTop, height,
padding = 0,
style = elem.style;
if(elem._length === elem.value.length) return;
elem._length = elem.value.length;
if(!isFirefox && !isOpera) {
padding = parseInt(getStyle(‘paddingTop‘)) + parseInt(getStyle(‘paddingBottom‘));
};
scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
elem.style.height = minHeight + ‘px‘;
if(elem.scrollHeight > minHeight) {
if(maxHeight && elem.scrollHeight > maxHeight) {
height = maxHeight - padding;
style.overflowY = ‘auto‘;
} else {
height = elem.scrollHeight - padding;
style.overflowY = ‘hidden‘;
};
style.height = height + extra + ‘px‘;
scrollTop += parseInt(style.height) - elem.currHeight;
document.body.scrollTop = scrollTop;
document.documentElement.scrollTop = scrollTop;
elem.currHeight = parseInt(style.height);
};
};
addEvent(‘propertychange‘, change);
addEvent(‘input‘, change);
addEvent(‘focus‘, change);
change();
};
</script>
================================
方案C:JQ实现textarea自适应(Id实现)
.text-adaption {
width: 300px;
height: 34px;
overflow: hidden;
padding: 5px 10px;
resize: none;
line-height: 24px;
font-size: 12px;
color: #666;
border: 1px solid #ccc;
outline: 0 none;
border-radius: 3px;
box-sizing: border-box;
}
<textarea id="text-adaption" class="text-adaption" rows="1"></textarea>
<script>
function $(id) {
return document.getElementById(id);
}
$("text-adaption").onkeyup = function() {
this.style.height = ‘auto‘;
this.style.height = this.scrollHeight + "px";
}
</script>
===========================================
方案D:JQ实现textarea自适应(class复用实现)
.text-adaption {
width: 300px;
height: 34px;
overflow: hidden;
padding: 5px 10px;
resize: none;
line-height: 24px;
font-size: 12px;
color: #666;
border: 1px solid #ccc;
outline: 0 none;
border-radius: 3px;
box-sizing: border-box;
}
<textarea id="text-adaption" class="text-adaption" rows="1" ></textarea>
<textarea class="text-adaption" rows="1" ></textarea>
<textarea class="text-adaption" rows="1" ></textarea
$(function(){
function getClass(c){
return document.getElementsByClassName(c);
}
var obj=getClass("text-adaption");
var len=obj.length;
for(var i=0;i<len;i++){
obj[i].onkeyup = function() {
this.style.height = ‘auto‘;
this.style.height = this.scrollHeight + "px";
};
}
});
=================================================
综合下载地址:http://files.cnblogs.com/files/leshao/textarea%E9%AB%98%E5%BA%A6%E8%87%AA%E9%80%82%E5%BA%94.rar
参考网友地址链接:http://blog.csdn.net/tianyitianyi1/article/details/49923069