textarea自动适应内容的高度

本文介绍了三种改进的文本区域高度自适应方案,包括滚动条调整、脚本自动计算高度和事件监听器实现高度变化,适用于不同浏览器环境。

方法一:

<textarea rows=1 cols=40 style='overflow:scroll;overflow-y:hidden;;overflow-x:hidden' 
onfocus="window.activeobj=this;this.clock=setInterval(function(){activeobj.style.height=activeobj.scrollHeight+'px';},200);" onblur="clearInterval(this.clock);"></textarea>

方法二:

<script>
var agt = navigator.userAgent.toLowerCase();
var is_op = (agt.indexOf("opera") != -1);
var is_ie = (agt.indexOf("msie") != -1) && document.all && !is_op;
function ResizeTextarea(a,row){
    if(!a){return}
    if(!row)
        row=5;
    var b=a.value.split("\n");
    var c=is_ie?1:0;
    c+=b.length;
    var d=a.cols;
    if(d<=20){d=40}
    for(var e=0;e<b.length;e++){
        if(b[e].length>=d){
            c+=Math.ceil(b[e].length/d)
        }
    }
    c=Math.max(c,row);
    if(c!=a.rows){
        a.rows=c;
    }
}
</script>
<textarea style="overflow: hidden;  font-family: Verdana,Arial; font-style: normal;  font-size: 13px; line-height: normal; " rows="4" cols="30" onfocus="javascript:ResizeTextarea(this,4);" onclick="javascript:ResizeTextarea(this,4);" onkeyup="javascript:ResizeTextarea(this,4);"></textarea>


方法三:

<script type="text/javascript">
    //基本函数*2
    var addHandler = window.addEventListener?
    function(elem,event,handler){elem.addEventListener(event,handler);}:
    function(elem,event,handler){elem.attachEvent("on"+event,handler);};
    var $ = function(id){return document.getElementById(id);}
    function autoTextArea(elemid){
        //新建一个textarea用户计算高度
        if(!$("_textareacopy")){
            var t = document.createElement("textarea");
            t.id="_textareacopy";
            t.style.position="absolute";
            t.style.left="-9999px";
            document.body.appendChild(t);
        }
        function change(){
            $("_textareacopy").value= $(elemid).value;
            $(elemid).style.height= $("_textareacopy").scrollHeight+$("_textareacopy").style.height+"px";
        }
        addHandler($("target"),"propertychange",change);//for IE
        addHandler($("target"),"input",change);// for !IE
        $(elemid).style.overflow="hidden";//一处隐藏,必须的。
        $(elemid).style.resize="none";//去掉textarea能拖拽放大/缩小高度/宽度功能
    }
 
    addHandler(window,"load",function(){
    autoTextArea("target");
    });
</script>
<textarea id="target" rows="" cols=""></textarea>
</body>
</html>

调试成功案例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<textarea rows=1 cols=40 style='overflow:scroll;overflow-y:hidden;;overflow-x:hidden' 
onfocus="window.activeobj=this;this.clock=setInterval(function(){activeobj.style.height=activeobj.scrollHeight+'px';},200);" onblur="clearInterval(this.clock);"></textarea><br/><br/>
<script>
var agt = navigator.userAgent.toLowerCase();
var is_op = (agt.indexOf("opera") != -1);
var is_ie = (agt.indexOf("msie") != -1) && document.all && !is_op;
function ResizeTextarea(a,row){
    if(!a){return}
    if(!row)
        row=5;
    var b=a.value.split("\n");
    var c=is_ie?1:0;
    c+=b.length;
    var d=a.cols;
    if(d<=20){d=40}
    for(var e=0;e<b.length;e++){
        if(b[e].length>=d){
            c+=Math.ceil(b[e].length/d)
        }
    }
    c=Math.max(c,row);
    if(c!=a.rows){
        a.rows=c;
    }
}
</script>
<textarea style="overflow: hidden;  font-family: Verdana,Arial; font-style: normal;  font-size: 13px; line-height: normal; " rows="4" cols="30" onfocus="javascript:ResizeTextarea(this,4);" onclick="javascript:ResizeTextarea(this,4);" onkeyup="javascript:ResizeTextarea(this,4);"></textarea>
</body>
</html>


在 UniApp 中实现 `<textarea>` 内容自动撑开高度,可以通过设置 `auto-height` 属性为 `true` 来启用自动调整功能。此属性会根据输入内容的多少动态调整文本区域的高度,从而提供更好的用户体验。 ### 基本实现方式 ```html <textarea auto-height="true" v-model="content" :style="{ maxHeight: '200rpx' }" @focus="onFocus" @blur="onBlur" > </textarea> ``` 在这个例子中: - `auto-height="true"` 是关键属性,它启用了自动调整高度的功能。 - `v-model="content"` 用于双向绑定输入的内容。 - `:style="{ maxHeight: '200rpx' }"` 设置了最大高度限制,防止内容过多时文本框无限扩展。 - `@focus` 和 `@blur` 是可选的事件处理程序,可以在用户聚焦或失去焦点时执行额外逻辑。 ### 高级样式控制 为了进一步优化视觉效果,可以结合 CSS 样式来微调行高、字体大小等细节: ```html <style scoped> textarea { max-height: 200rpx; line-height: 40rpx; font-size: 28rpx; padding: 10rpx; border-width: 2rpx; border-style: solid; border-color: #eaeaea; } </style> ``` 上述样式定义了: - `max-height`: 文本框的最大高度。 - `line-height`: 行高,影响每行文字之间的间距。 - `font-size`: 字体大小。 - `padding`: 内边距,确保内容与边框之间有足够的空间。 - `border-width`, `border-style`, `border-color`: 边框样式,提升整体美观性。 ### 解决 iOS 上的高度异常问题 在某些情况下,特别是在 iOS 设备上,可能会出现文本框在有内容高度显示异常的问题。为了解决这个问题,可以在样式中添加 `border-width` 属性,以确保文本框在不同设备上的表现一致[^2]。 ```html <textarea auto-height="true" v-model="content" :style="{ maxHeight: '200rpx' }" @focus="onFocus" @blur="onBlur" style="border-width: 2rpx;" > </textarea> ``` 此外,还可以通过 JavaScript 方法监听 `focus` 和 `blur` 事件,并根据需要手动调整文本框的高度或其他相关属性: ```javascript export default { data() { return { content: '', isFocused: false }; }, methods: { onFocus(e) { this.isFocused = true; // 可以在这里进行其他操作,例如记录键盘高度 }, onBlur(e) { this.isFocused = false; // 失去焦点时可以重置某些状态 } } }; ``` ### 总结 通过使用 `auto-height="true"` 属性并结合适当的 CSS 样式和事件处理逻辑,可以在 UniApp 中轻松实现 `<textarea>` 内容自动撑开高度的效果。同时,针对特定平台(如 iOS)可能出现的问题,可以通过添加 `border-width` 等样式属性来解决高度异常的情况,确保跨平台的一致性和稳定性。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值