【问题标题】:Dynamically resize textarea width and height to contain text动态调整文本区域的宽度和高度以包含文本
【发布时间】:2014-09-12 22:31:54
【问题描述】:

我有一个包含 textarea 元素的 div。 div 的大小是固定的,但如果输入了足够的文本,滚动条会显示。目前,textarea 的高度可以正确地动态增长和缩小,但宽度却不能。

我一直在修改这里给出的代码:http://alistapart.com/article/expanding-text-areas-made-elegant 并且已经到了这一点(在 jsfiddle 中显示):http://jsfiddle.net/fayu5sh2/2/

它目前的工作方式是将 textarea 设置为 div 的 100% 宽度和高度,并将其内容馈送到隐藏跨度中,这会改变包含 div 的高度(按下回车时)和宽度.虽然跨度正常运行,但 textarea 不保持宽度:100%。可以这样做吗?

隐藏的 span 当前可见以显示其内容在做什么,textarea 中的文本应直接位于 span 中的文本之上。

这里是html:

<div id="containing_box">
    <div class="expandingArea">
        <pre><span></span><br></pre>
        <textarea></textarea>
    </div>
</div>

这里是javascript:

$(document).ready(

    function() {

        $('div.expandingArea').each(function() {
            var area = $('textarea', $(this));
            var span = $('span', $(this));

            area.bind('input', function() {
                span.text(area.val());
            });

            span.text(area.val());

            $(this).addClass('active');
        });    
    }
);

和 CSS:

#containing_box {
    width: 300px;
    height: 200px;
    overflow: auto;
    border: 1px solid;
}

textarea, 
pre, p {
  margin: 0;
  padding: 0;
  outline: 0;
  border: 0;
}

.expandingArea {
  position: relative;
  border: 1px solid #888;
  background: #fff;
}

.expandingArea > textarea,
.expandingArea > pre {
    padding: 5px;
    background: transparent;
    white-space: pre;
}

.expandingArea > textarea {
  /* The border-box box model is used to allow
   * padding whilst still keeping the overall width
   * at exactly that of the containing element. */

  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
      -ms-box-sizing: border-box;
          box-sizing: border-box;


  /* This height is used when JS is disabled  */
  height: 100px;
  width: 100px;
}

.expandingArea.active > textarea {
  /* Hide any scrollbars */
  overflow: hidden;
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  /* Remove WebKit user-resize widget */
  resize: none;
}

.expandingArea > pre {
  /* display: none;*/
  color: blue;
}
.expandingArea.active > pre {
  display: block;
  /* Hide the text; just using it for sizing */
  /* visibility: hidden; */
}

【问题讨论】:

  • @sbjumani 我发现的所有线程似乎只处理调整文本区域高度的大小,不幸的是不是宽度......
  • 是要避免换行,还是希望宽高始终相等,所以 textarea 是正方形?
  • 把你的文本放在一个容器中,并将最大宽度设置为 100%。允许宽度由父级控制并由您管理高度。两者都需要计算听起来有点奇怪。
  • @RickHitchcock 不,它不需要保持正方形,是的,我想避免包装。如果添加换行符,我想要的是 textarea 的高度增加,否则如果您继续在同一行上输入,则只有宽度增加。

标签: javascript jquery html css


【解决方案1】:

您可以通过在input 事件中监控scrollWidthscrollHeight 来动态调整textarea 的大小。

此代码调整所有 textareas 的大小,同时保持 50 像素的最小宽度和高度:

$('textarea').on('input', function() {
  $(this)
    .width (50)
    .height(50)
    .width (this.scrollWidth)
    .height(this.scrollHeight);
});

请注意,widthheight 首先设置为其初始值以强制滚动。

textareawrap 属性设置为“off”:

<textarea wrap="off"></textarea>

并将其样式设置为overflow: hidden:

textarea {
  overflow: hidden;
}

片段:

$('textarea').on('input', function() {
  $(this)
    .width (50)
    .height(50)
    .width (this.scrollWidth)
    .height(this.scrollHeight);
});
textarea {
  overflow: hidden;
  width: 50px;
  height: 50px;
  padding: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea wrap="off" autofocus></textarea>

【讨论】:

  • 这让我更接近我所追求的。我采用了这种方法并简化了我的代码(jsfiddle)。虽然为什么 textarea 边框会在溢出中消失?
  • 不确定您指的是我的代码还是您的代码?您的小提琴明确隐藏了 textarea 边框,但它应该在我的中可见。
  • 我明白你的意思,我看到的边框是扩展区域 div 边框,而不是 textarea 边框。
  • @Rick Hitchcock 太棒了...顺便说一句,如何在更改内部内容的字体大小时调整此文本区域的大小?
  • @RyanTran,应该仍然可以工作,但您可能需要触发事件,例如 $('textarea').trigger('input')
【解决方案2】:

在 javascript 中添加这个

$('textarea').on('keyup',function(){
  var spanwidth = $('span').css('width')
  $('textarea').css('width',spanwidth) 
})

这是小提琴http://jsfiddle.net/fayu5sh2/5/

如果页面上有多个这些,则需要使用 ID 或类,而不是一般的“span”和“textarea”

【讨论】:

  • 我最初尝试了小提琴,发现它没有按预期工作。问题是这个函数的内容应该在我已经拥有的每个函数中。仍在解决其他几个问题,主要与边界不正常有关。
猜你喜欢
  • 2016-06-21
  • 1970-01-01
  • 2014-09-19
  • 2014-09-19
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
相关资源
最近更新 更多