【发布时间】:2018-07-18 08:42:18
【问题描述】:
我正在创建一个在页面中水平和垂直居中的 TextArea。鉴于我需要将 TextArea 垂直居中,我不能让 textarea 为 100w 和 100h,我需要它从一个小的高度开始,然后随着用户键入而增长。我还需要一个单击捕获器,因此如果用户单击 textarea 周围的区域,则 textarea 会聚焦。 (灰色的 bkg 仅用于调试目的)
我在这里有一个关于 CodePen 的演示:https://codepen.io/anon/pen/OQbvxa
autosize(document.getElementById("note"));
$( ".textAreaClickCapturer" ).mouseup(function() {
$( "#note" ).focus();
});
html, body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
height: 100%;
width: 100%;
}
.page-body.fullScreen {
display: flex;
flex: 1 1;
flex-direction: column;
height: 100%;
width: 100%;
background: #EFEFEF;
text-align: left;
padding: 0!important;
align-items: normal;
justify-content: normal;
}
form {
width: 100%;
height: 100%;
display: flex;
align-items: center;
}
form .textAreaClickCapturer {
width: 100%;
height: 100%;
display: flex;
overflow: hidden;
align-items: center
}
form .field {
width: 100%;
margin: 0;
padding: 24px;
clear: both;
max-height: max-content;
height: 100%;
align-items: center;
}
textarea {
border: none;
border-radius: 0;
font-size: 21px;
line-height: 28px;
padding: 0;
vertical-align: top;
width: 100%;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/jackmoore/autosize/master/dist/autosize.min.js"></script>
<div class="page-body fullScreen ">
<form action="/">
<div class="textAreaClickCapturer" role="presentation">
<div class="field">
<textarea id="note" placeholder="Say something..." rows="3"></textarea>
</div>
</div>
</form>
</div>
当您输入多行时,TextArea 确实会很好地增长,但最终,TextArea 会增长到超过屏幕的大小,导致屏幕中断。
我怎样才能让 TextArea 增长到 TextArea 不会出现在页面后面并中断但有一个工作溢出来处理大量文本的地方?
谢谢!
【问题讨论】:
标签: javascript html css flexbox