Input 多行文字实现请教?

// 我要做个判断 如果 i.attr.format.align == mulit_line      那么可以进行多行输入,并且高度自适应
 <input class="cell-input" type="text" v-model=i.attr.value
            @blur="inputBlurCheck(i)" @input="testInput(i)" 
            :style="{
              'fontFamily': i.attr.font_family,
              'fontSize': i.attr.font_size + 'px',
              'color': i.attr.format.color ? i.attr.format.color : i.attr.font_color,
              'backgroundColor': i.attr.background_color,
              'letterSpacing': i.attr.letter_spacing,
              'textAlign': i.attr.align_h,
              'fontWeight': i.attr.font_weight,
              'fontStyle': i.attr.font_style,
              'textDecoration': i.attr.text_decoration,
            }" />


<input v-if ='i.attr.format.align == mulit_line' />

<textarea v-else ... />
这个方法感觉很low

input没法换行,直接textarea,不是多行的话input事件中替换掉输入值的回车(overflow:hidden+white-space:nowrap),但是鼠标会无法像input那样可以左右移动,需要用键盘左右箭头来移动光标,所以还是用做切换好些
自适应的话需要用js来控制textarea的高度,根据滚动高度重新设置高度
简单示例如下


<!doctype html>
<script src="https://unpkg.com/vue@next" data-ver="3"></script>
<style>
    textarea{resize:none;line-height:20px;box-sizing:border-box;height:26px;padding:2px;outline:none;overflow:hidden;}
    textarea.singleLine{white-space:nowrap}
</style>
<div id="app">
    <div v-for="i in arr">
        <textarea :ref="i.attr.id"  v-model="i.attr.value" @input="testInput(i)" :class="i.attr.format.align=='mulit_line'?'':'singleLine'" :placeholder="i.attr.format.align"></textarea>
    </div>
</div>
<script>
    var app = {
        data() {
            return {
                arr: [
                    {
                        attr: {
                            id: 'ta1',
                            value: '',
                            format: { align: 'single' }
                        }
                    },
                    {
                        attr: {
                            id: 'ta2',
                            value: '',
                            format: { align: 'mulit_line' }
                        }
                    }
                ]
            };
        },
        methods: {
            testInput(i) {
                let ta = this.$refs[i.attr.id][0];
                if (i.attr.format.align == 'mulit_line') {
                    ta.style.height = '26px';//恢复默认高度,要不减少内容不会缩回去
                    ta.style.height = ta.scrollHeight + 2 + 'px';
                }
                else {
                    i.attr.value = i.attr.value.replace(/[\r\n]/g, '')
                }
            }
        }
    };
    Vue.createApp(app).mount('#app')
</script>

textarea没办法高度自适应,只能自己封装
https://blog.csdn.net/m553366999/article/details/124973293


            #textarea {
            
                display: block;
            
                margin:0 auto;
            
                overflow: hidden;
            
                width: 550px;
            
                font-size: 14px;
            
                height: 18px;
            
                line-height: 24px;
            
                padding:2px;
            
            }
            
            textarea {
            
                outline: 0 none;
            
                border-color: rgba(82, 168, 236, 0.8);
            
                box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1), 0 0 8px rgba(82, 168, 236, 0.6);
            
            }
<div class="textarea" contenteditable="true"><br /></div>

 var text = document.getElementById("textarea");
    
            autoTextarea(text);// 调用
            var autoTextarea = function (elem, extra, maxHeight) {
            
                    extra = extra || 0;
            
                    var isFirefox = !!document.getBoxObjectFor || 'mozInnerScreenX' in window,
            
                    isOpera = !!window.opera && !!window.opera.toString().indexOf('Opera'),
            
                            addEvent = function (type, callback) {
            
                                    elem.addEventListener ?
            
                                            elem.addEventListener(type, callback, false) :
            
                                            elem.attachEvent('on' + type, callback);
            
                            },
            
                            getStyle = elem.currentStyle ? function (name) {
            
                                    var val = elem.currentStyle[name];
            
                                    if (name === 'height' && val.search(/px/i) !== 1) {
            
                                            var rect = elem.getBoundingClientRect();
            
                                            return rect.bottom - rect.top -
            
                                                    parseFloat(getStyle('paddingTop')) -
            
                                                    parseFloat(getStyle('paddingBottom')) + 'px';        
            
                                    };
            
                                    return val;
            
                            } : function (name) {
            
                                            return getComputedStyle(elem, null)[name];
            
                            },
            
                            minHeight = parseFloat(getStyle('height'));
            
                    elem.style.resize = 'none';
            
                    var change = function () {
            
                            var scrollTop, height,
            
                                    padding = 0,
            
                                    style = elem.style;
            
                            if (elem._length === elem.value.length) return;
            
                            elem._length = elem.value.length;
            
                            if (!isFirefox && !isOpera) {
            
                                    padding = parseInt(getStyle('paddingTop')) + parseInt(getStyle('paddingBottom'));
            
                            };
            
                            scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
            
                            elem.style.height = minHeight + 'px';
            
                            if (elem.scrollHeight > minHeight) {
            
                                    if (maxHeight && elem.scrollHeight > maxHeight) {
            
                                            height = maxHeight - padding;
            
                                            style.overflowY = 'auto';
            
                                    } else {
            
                                            height = elem.scrollHeight - padding;
            
                                            style.overflowY = 'hidden';
            
                                    };
            
                                    style.height = height + extra + 'px';
            
                                    scrollTop += parseInt(style.height) - elem.currHeight;
            
                                    document.body.scrollTop = scrollTop;
            
                                    document.documentElement.scrollTop = scrollTop;
            
                                    elem.currHeight = parseInt(style.height);
            
                            };
            
                    };
            
                    addEvent('propertychange', change);
            
                    addEvent('input', change);
            
                    addEvent('focus', change);
            
                    change();
            
            };

多行直接用

<textarea cols="行数" rows="列数">内容</textarea>

参考:

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632