Vue中可根据内容自适应改变高度的textarea文本框
如图所示,当
textarea
里的内容超过一行后,会出现滚动条并隐藏前一行的内容,特别是在移动端使用到textarea多行文本输入的话,这样显示其实是很不友好的。所以要做一个可根据内容改变高度的textarea的组件。
踩坑尝试
利用rows
属性来改变高度:

W3C HTML <textarea> 标签
踩坑时的思路:
- 给
textarea
加上rows
属性,并双向绑定在data
的rows
上。如:<textarea ref="textarea" :rows="rows" v-model="value" class="textarea"></textarea> - 获取初始页面时候
textarea
的高度,这就是一行的高度oneHeight
; - 通过
vue
的watch
数据监听,当textarea
的内容发生变化时触发,获取textarea
的scrollHeight
,再除以oneHeight
求整数然后加一就是rows
的数量。
踩坑感想:
这样做是可以实现当内容变多,行数跟着变多的情况的,但是,当内容变少,scrollHeight
是不会减少的!所以rows
也不会变,一旦被撑大,就再也缩不回去。。。。显然,这不是我想要的效果。
正确姿势
猛然回想起ElementUI上就有可根据内容调整高度的组件ElementUI input!
然后就去扒源码看看是怎么实现的,结果都已经封装好了,太棒了,直接下载来引用就行了!
饿了么组件的源码github地址:
https://github.com/ElemeFE/element/blob/dev/packages/input/src/calcTextareaHeight.js
下面是引用了ElementUI
的input
组件的calcTextareaHeight
方法的MyTextarea.vue
:
1 <template> 2 <div class="my-textarea"> 3 <textarea ref="textarea" :style="{'height': height}" v-model="value" class="textarea" ></textarea> 4 </div> 5 </template> 6 7 <script> 8 import calcTextareaHeight from '@/assets/calcTextareaHeight'; 9 10 export default { 11 name: 'my-textarea', 12 data() { 13 return { 14 // textarea内容 15 value: '', 16 // 动态高度 17 height: '30px' 18 } 19 }, 20 watch: { 21 value() { 22 this.getHeight(); 23 } 24 }, 25 methods: { 26 getHeight() { 27 this.height = calcTextareaHeight(this.$refs.textarea, 1, null).height; 28 } 29 } 30 } 31 </script> 32 33 <style scoped> 34 .my-textarea .textarea { 35 display: inline-block; 36 width: 400px; 37 /*height: 30px;*/ 38 line-height: 30px; 39 font-size: 30px; 40 resize: none; 41 } 42 </style>