首页 > 解决方案 > 如何使用 CSS 强制 textarea 垂直调整大小?

问题描述

我有一个textarea元素,我希望它有一个默认高度(例如 340 像素),但可以垂直调整大小到浏览器窗口高度的 70%(当我输入大量文本时,例如应对和粘贴这个问题应该使它垂直更大)。

我尝试resize: vertical在 CSS 中设置参数并max-height如下使用,但它仍然没有调整大小。

我做错了什么,我该如何解决?

#content {
    position: relative;
    top: 0px;
/*
    max-width: 320px;
*/
    margin: 0 0 0 0;
    line-height: 1.6em;
    z-index: 1;
    height: 100%;
}

#statements {
    position: absolute;
    bottom: 0px;
    left: 0px;
    padding-bottom: 0px;
    max-height: 70%;
    background-color: rgba(255, 255, 255, 0);
    max-width: 340px;
    padding-left: 50px;
    z-index: 10;
    overflow: hidden;
/*    pointer-events: none;*/
}

#entryform {
    position: relative;
    background-color: var(--entry-form-background);
    width: 340px;
    border-radius: 8px;
    border-top-left-radius: 0px!important;
    padding-top: 10px;
    height: 100%;
    padding-bottom: 20px;
    padding-left: 10px;
    margin-left: -10px;
    margin-bottom: 0px;
    z-index: 10!important;
    display: block;
}

textarea {
    font: 16px 'IstokWeb-Bold',sans-serif;
    position: relative;
    resize: vertical;
    width: 92%;
    max-height: 100%;
    overflow: auto; /* 1 */
    vertical-align: top; /* 2 */
    padding: 0.5em 0.6em;
    display: block;
    margin: 0.25em 0;
    border: 1px solid #e6e6e6;
    box-shadow: inset 0 1px 3px #e6e6e6;
    border-radius: 6px;
    -webkit-transition: 0.3s linear border;
    -moz-transition: 0.3s linear border;
    -ms-transition: 0.3s linear border;
    -o-transition: 0.3s linear border;
    transition: 0.3s linear border;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    -webkit-font-smoothing: antialiased;
    z-index: 10;
}
<div id='statements'>

  <div id="entryform" class="editorpane">
      
    <div id="edit-panel">
    
    <form action='/post' name='submitform' id="submitform" method='post'>

    <textarea name='entry[body]' id="statement" placeholder='type in some words or #hashtags to see how they connect or copy and paste your notes or text here'></textarea>

            
    <input type='submit' id="submitbutton" name="btnSubmit" value="save" class="pure-button pure-button-primary">
          

    </form>
    
    </div>

  </div>
</div>

标签: cssresizetextareaautoresize

解决方案


您可以使用带有事件侦听器的 javascriptkeyup和/或change这样当您粘贴文本时,它会将 textarea 高度调整为element.scrollHeight.

let textarea = document.getElementById('textArea');

textarea.addEventListener('change', resizetextArea);
textarea.addEventListener('keyup', resizetextArea);

function resizetextArea(){
  textarea.style.height = `${textarea.scrollHeight}px`;
}
#content {
  position: relative;
  top: 0px;
  /*
    max-width: 320px;
*/
  margin: 0 0 0 0;
  line-height: 1.6em;
  z-index: 1;
  height: 100%;
}

#statements {
  position: absolute;
  bottom: 0px;
  left: 0px;
  padding-bottom: 0px;
  max-height: 70%;
  background-color: rgba(255, 255, 255, 0);
  max-width: 340px;
  padding-left: 50px;
  z-index: 10;
  overflow: hidden;
  /*    pointer-events: none;*/
}

#entryform {
  position: relative;
  background-color: var(--entry-form-background);
  width: 340px;
  border-radius: 8px;
  border-top-left-radius: 0px!important;
  padding-top: 10px;
  height: 100%;
  padding-bottom: 20px;
  padding-left: 10px;
  margin-left: -10px;
  margin-bottom: 0px;
  z-index: 10!important;
  display: block;
}

textarea {
  font: 16px 'IstokWeb-Bold', sans-serif;
  position: relative;
  resize: vertical;
  width: 92%;
  max-height: 100%;
  overflow: auto;
  /* 1 */
  vertical-align: top;
  /* 2 */
  padding: 0.5em 0.6em;
  display: block;
  margin: 0.25em 0;
  border: 1px solid #e6e6e6;
  box-shadow: inset 0 1px 3px #e6e6e6;
  border-radius: 6px;
  -webkit-transition: 0.3s linear border;
  -moz-transition: 0.3s linear border;
  -ms-transition: 0.3s linear border;
  -o-transition: 0.3s linear border;
  transition: 0.3s linear border;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  -webkit-font-smoothing: antialiased;
  z-index: 10;
}
<p>
  <b>Copy and paste this text:</b> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
<div id='statements'>

  <div id="entryform" class="editorpane">

    <div id="edit-panel">


      <form action='/post' name='submitform' id="submitform" method='post'>

        <textarea id="textArea" name='entry[body]' id="statement" style="overflow:hidden" placeholder=''></textarea>


        <input type='submit' id="submitbutton" name="btnSubmit" value="save" class="pure-button pure-button-primary">


      </form>

    </div>

  </div>
</div>


推荐阅读