怎么讓html表單元素自動充滿瀏覽器窗口,並且自適應瀏覽器窗口變化?


  • 技術:vue + typescript
  • 思路:將“ body的高度 - 死值 ”得到高度,按需要分給目標元素即可。
  • 例子:
  h1 = '400px';//textarea 1初始高度
  h2= '400px';//textarea 2初始高度
  beforeMount() {
    this.h1 =  this.h2 = this.GetHeight() + 'px';//textArea高度 
  }
  
  async mounted() { 
    let that = this as any;
    window.onresize = () => {
      this.h1 =  this.h2 = that.GetHeight()+ 'px';//調整瀏覽器大小時改變textArea高度 
    }
  }
  
  //textArea自適應頁面高度 
  GetHeight() {
    let p=30*3;//圖中前3行元素高度(死值)
    let nH = (document.body.clientHeight - p);
    let h = nH / 2;//剩下高度均給文本框
    if (h < 130) {
      h = 130;
    } else if (h > 600) {
      h = 600;
    }
    return h;
  }

【html 代碼】
<textarea :style="{'margin-left': '10px','outline': 'none','height':h1+'px'}" placeholder="請輸入內容"></textarea>


免責聲明!

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



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