textarea自动增高并隐藏滚动条

本文介绍了一种使用JavaScript实现的自动调整高度的文本输入框功能,通过监听输入变化实时更新元素高度,并确保滚动条始终处于顶部。

<script type="text/javascript">
function autoHeight(self){

$('#testbox').html($(self).val());
var height = $('#testbox').outerHeight();
if(height<20){
return;
}
$(self).scroll(function(){
$(self).scrollTop(0);
});
$(self).outerHeight(height);

}
</script>
<textarea class="autoheight" id="tValue" style="overflow-y:hidden; height:20px;" onpropertychange="autoHeight(this)" oninput="autoHeight(this)" onkeyup="autoHeight(this)" onkeydown="autoHeight(this)" ></textarea>
<pre class="autoheight" style="" id="testbox">&nbsp;</pre>
<style type="text/css">
.autoheight{ padding: 5px; border:1px solid #666; line-height: 20px; font-size: 14px; }
</style>

转载于:https://www.cnblogs.com/justlancer/archive/2012/09/25/2701762.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值