小程序實現textarea隨輸入的文字行數變化高度自動增加


參考鏈接:https://blog.csdn.net/liuwengai/article/details/78987957

該實現方法是根據上面的鏈接改編為小程序的實現,代碼如下:

wxml:

<view class="text-box">
      <text>{{currentInput}}</text>
      <textarea class="weui-textarea" placeholder="請輸入文本" bindinput="getInput" maxlength="1000"/>
</view>

wxss:

 

.text-box{
    width:100%; 
    position: relative;
    min-height:80rpx;
   margin-top:17rpx;
}
.text-box text{
   display:block;
   visibility:hidden;
   word-break:break-all;
   word-wrap:break-word;
}
.text-box .weui-textarea{
    height:100%;
    position: absolute;
    left:0;
    top:0; 
    overflow-y:hidden;
    word-break:break-all;
    word-wrap:break-word;
}

 

js:

Page({
  data: {
    currentInput:''
  },
  getInput:function(e){
    this.setData({
      currentInput: e.detail.value
    })
  }
})

實現效果如圖:

 

提醒:默認textarea應該是200個字,如果想要增加字數限制,使用maxlength屬性

擴展:如果想給textarea輸入的文字加刪除線,只需把text的

visibility:hidden; 屬性去掉,給textarea加一個透明的顏色就可以了。
透明顏色:color:rgba(52, 52, 52,0.5);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM