只使用纯粹的飞镖:html:
var textArea = querySelector('textarea');
textArea.onInput.listen((_) {
// shrink the textarea when needed
textArea.style.height = 'auto';
// set the height to scrollHeight plus some correction
var correction = textArea.offsetHeight - textArea.clientHeight;
textArea.style.height = '${textArea.scrollHeight - correction}px';
});
Run Code Online (Sandbox Code Playgroud)
你也可以制作一个angular2指令:
@Directive(
selector: 'textarea[autogrow]',
host: const {
'(input)': 'onInput(\$event.target)'
}
)
class AutogrowDirective {
onInput(TextAreaElement textArea) {
// shrink the textarea when needed
textArea.style.height = 'auto';
// set the height to scrollHeight plus some correction
var correction = textArea.offsetHeight - textArea.clientHeight;
textArea.style.height = '${textArea.scrollHeight - correction}px';
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
180 次 |
| 最近记录: |