我有一个令人满意的div grows as the user types。
我现在需要转换高度,以便当用户按下 Enter 时,div会慢慢增长。
这是我正在寻找的动画(但是当用户创建新行而不是焦点时):
这是我的(可能是天真的)尝试:
CSS:
div[contenteditable]{
border: 1px solid black;
max-height: 200px;
overflow: auto;
transition : all 300ms ease;
}
HTML:
<div contenteditable>
Testing <br/> one two three
</div>
我是否可以仅使用HTML / CSS实现此目的,还是必须使用JS?
答案 0 :(得分:4)
@keyframes lineInserted {
from {
height: 0;
}
to {
height: 20px; /* cons: hardcoded height */
}
}
div[contenteditable] > div {
animation-duration: 300ms;
animation-name: lineInserted;
transition: height 300ms;
}
div[contenteditable] {
border: 1px solid black;
max-height: 200px;
overflow: auto;
transition: all 300ms ease;
}
<div contenteditable>
Testing
<br/>one two three
</div>
答案 1 :(得分:1)
基于Orland的答案的最终解决方案。谢谢你,bootstrap!
HTML
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div contenteditable class="form-control">
Testing <br/> one two three
</div>
CSS:
@keyframes lineInserted {
from { height: 0; }
to { height: 20px; } /* cons: hardcoded height */
}
div[contenteditable] > div {
animation-duration: 200ms;
animation-name: lineInserted;
}
div[contenteditable]{
height : auto !important;
overflow: auto;
line-height : 20px;
}