想象一下我有以下内容
div{
padding: 20px;
}
<div style="display:flex; background: gold; flex-direction: column;">
<textarea>Do NOT expand this</textarea>
<textarea class="expand">Expand this baby</textarea>
</div>
我希望当文本区域“expand”上的内容创建垂直溢出以扩展父 div 时,not创建垂直滚动。
我认为仅使用 CSS 无法完成此操作,并且由于您不需要 jQuery,因此这里是一个纯 JS 解决方案。这个想法是在每次更新内容时计算文本区域的高度(调整大小)
var tex = document.querySelector('textarea.expand');
tex.addEventListener('keydown', resize);
function resize() {
setTimeout(function() {
tex.style.height = 'auto'; //needed when you remove content so we reduce the height
tex.style.height = tex.scrollHeight + 'px';
}, 0);
}
div {
padding: 20px;
}
<div style="display:flex; background: gold; flex-direction: column;">
<textarea>Do NOT expand this</textarea>
<textarea class="expand">Expand this baby</textarea>
</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)