textarea高度自适应,随文字增加自动撑开
2017-12-28 19:19
656 查看
需求: 随文字增多,将textarea自动撑开。
解决思路: pre标签能保持文本的格式,将textarea的文本内容和样式严格复制一份给pre标签,pre的高度值就为我们所需的textarea的高度值。然后将pre隐藏。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<style>
#fit1{
width: 674px;
margin: 0 0 30px 0;
padding: 10px 10px 20px;
border: 1px solid #e6e6e6;
min-height: 88px;
resize: none;
line-height: 24px;
border-radius: 3px;
font-size: 14px;
font-family: '微软雅黑', Helvetica, sans-serif;
overflow: hidden;
word-wrap: break-word;
white-space: pre-wrap;
}
.hide{
visibility: hidden;
position: absolute;
z-index: -100;
}
#fit2{
width: 674px;
padding: 10px 10px 20px;
min-height: 88px;
resize: none;
border: 1px solid #e6e6e6;
line-height: 24px;
border-radius: 3px;
font-size: 14px;
font-family: '微软雅黑', Helvetica, sans-serif;
overflow: hidden;
word-wrap: break-word;
}
</style>
<body>
<div>
<pre id="fit1" class="hide"></pre>
<textarea id="fit2"></textarea>
</div>
<script>
//高度自适应
document.onkeydown = function () {
var textarea = document.getElementById('fit2')
var pre = document.getElementById('fit1');
pre.innerHTML = textarea.value;
var realHeight = pre.offsetHeight;//offsetHeight = height + padding + border
if(realHeight > 120) textarea.style.height = (realHeight + 24 -32) + 'px'; //加24为一行的高度,减32为padding和border
else textarea.style.height = realHeight - 32 + 'px';
}
</script>
</body>
</html>
解决思路: pre标签能保持文本的格式,将textarea的文本内容和样式严格复制一份给pre标签,pre的高度值就为我们所需的textarea的高度值。然后将pre隐藏。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<style>
#fit1{
width: 674px;
margin: 0 0 30px 0;
padding: 10px 10px 20px;
border: 1px solid #e6e6e6;
min-height: 88px;
resize: none;
line-height: 24px;
border-radius: 3px;
font-size: 14px;
font-family: '微软雅黑', Helvetica, sans-serif;
overflow: hidden;
word-wrap: break-word;
white-space: pre-wrap;
}
.hide{
visibility: hidden;
position: absolute;
z-index: -100;
}
#fit2{
width: 674px;
padding: 10px 10px 20px;
min-height: 88px;
resize: none;
border: 1px solid #e6e6e6;
line-height: 24px;
border-radius: 3px;
font-size: 14px;
font-family: '微软雅黑', Helvetica, sans-serif;
overflow: hidden;
word-wrap: break-word;
}
</style>
<body>
<div>
<pre id="fit1" class="hide"></pre>
<textarea id="fit2"></textarea>
</div>
<script>
//高度自适应
document.onkeydown = function () {
var textarea = document.getElementById('fit2')
var pre = document.getElementById('fit1');
pre.innerHTML = textarea.value;
var realHeight = pre.offsetHeight;//offsetHeight = height + padding + border
if(realHeight > 120) textarea.style.height = (realHeight + 24 -32) + 'px'; //加24为一行的高度,减32为padding和border
else textarea.style.height = realHeight - 32 + 'px';
}
</script>
</body>
</html>
相关文章推荐
- Flex TextArea 根据文字内容自动增加高度
- iOS UITextView 高度随文字自动增加,并跟随键盘移动(二)
- IOS UILabel自适应里面的文字,自动调整宽度和高度的
- iOS UITextView 高度随文字自动增加,并跟随键盘移动(一)
- textarea自动换行,且文本框根据输入内容自适应高度
- IOS UILabel自适应里面的文字,自动调整宽度和高度的
- UILabel自适应里面的文字,自动调整宽度和高度
- textarea根据内容自动增加高度
- 随着文字内容增加,div的高度也跟着增加,并且文字自动换行
- UITextView高度随文字自动增加类似于微信
- textarea高度自适应自动展开
- textarea高度自适应(随着内容增加高度增加)
- React textarea自适应文字高度伸缩 超简单版
- textarea随输入文本增加,其高度自动加大
- 设置textView根据输入的文字自动增加高度
- UILabel自适应里面的文字,自动调整宽度和高度
- IOS UILabel自适应里面的文字,自动调整宽度和高度的
- iOS UILabel自适应里面的文字,自动调整宽度和高度
- textarea自动换行与自适应高度
- TEXTAREA高度自适应自动展开