Textarea文本框根据输入内容自适应高度
本文将要实现textarea文本框根据输入内容自适应高度。有时候,例如:实现评论功能中的评论输入框。一开始的高度是固定,当换行或者文字输入到一定字数后,文本框的高度希望是跟随文字而增加。
按照平常的写法,textarea文本框是不可能根据内容而自适应高度的,它只可以通过用户自己手动拉伸而改变大小
html文件(index.html)
<!DOCTYPE html>
<html>
<head>
<title>普通的textarea文本框</title>
<meta charset="utf-8" />
<style>
h2{
text-align: center;
margin:50px auto;
}
.textarea {
display: block;
margin:0 auto;
overflow: hidden;
width: 550px;
font-size: 14px;
height: 50px;
line-height: 24px;
padding:2px;
outline: none;
margin: 30px auto;
}
</style>
</head>
<body>
<h2>普通的textarea文本框</h2>
<textarea class="textarea" placeholder="回复内容"></textarea>
</body>
</html>
那么,如果实现textarea文本框根据输入内容自适应高度呢?这里就需要添加一些js代码了
js文件(autoTextarea.js)
/**
* 文本框根据输入内容自适应高度
* @elem {HTMLElement} 输入框元素
* @extra {Number} 设置光标与输入框保持的距离(默认0)
* @maxHeight {Number} 设置最大高度(可选)
*/
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();
};
html文件(index.html)
<!DOCTYPE html>
<html>
<head>
<title>textarea文本框根据输入内容自适应高度</title>
<meta charset="utf-8" />
<script type="text/javascript" src="autoTextarea.js"></script>
<style>
h2{
text-align: center;
margin:50px auto;
}
.textarea {
display: block;
margin:0 auto;
overflow: hidden;
width: 550px;
font-size: 14px;
height: 50px;
line-height: 24px;
padding:2px;
outline: none;
margin: 30px auto;
}
</style>
</head>
<body>
<h2>textarea文本框根据输入内容自适应高度</h2>
<textarea class="textarea" id="textarea" placeholder="回复内容"></textarea>
<script type="text/javascript">
var text = document.getElementById("textarea");
autoTextarea(text);// 调用
</script>
</body>
</html>