实现可调整大小的文本区域?

发布于 2024-07-06 00:24:41 字数 274 浏览 5 评论 0原文

Stackoverflow 如何实现可调整大小的文本区域?

这是他们自己推出的东西,还是我可以轻松附加到我网站上的文本区域的公开组件?

我发现了这个问题,但它并没有完全达到我想要的效果。

autosizing-textarea

这更多地讨论了自动调整文本区域的大小,而我想要一个可以向上拖动的小抓取区域向下。

How does Stackoverflow implement the resizable textarea?

Is that something they rolled themselves or is it a publicly available component that I can easily attach to textareas on my sites?

I found this question and it doesn't quite do what I want.

autosizing-textarea

That talks more about automatically resizing textareas whereas I want the little grab-area that you can drag up and down.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不如归去 2024-07-13 00:24:41

我最近需要类似的功能。 它名为 Autogrow,它是令人惊叹的 jQuery

I needed a similar functionality recently. Its called Autogrow and it is a Plugin of the amazing jQuery library

温柔戏命师 2024-07-13 00:24:41

起初我认为这是 Wysiwym Markdown 编辑器 的内置功能,但 Shog9 是正确的:它是根本没有内置,但是由 jQuery 插件 TextAreaResizer 提供(我被用来检查 编辑器演示,因为 Google Chrome 本身在文本区域上添加了可扩展功能 - 与 Safari 浏览器非常相似)。

At first I believed it was a built-in feature of the Wysiwym Markdown editor, but Shog9 is correct: it's not baked-in at all, but is courtesy of the jQuery plugin TextAreaResizer (I was lead astray by the browser was using to check on the editor demo because Google Chrome itself adds the expandable functionality on textareas—much like the Safari browser does).

浮云落日 2024-07-13 00:24:41

StackOverflow 使用 jQuery 插件来完成此操作:TextAreaResizer

验证这一点很容易 - 只需从站点中提取 JS 文件即可。

历史注释:最初编写此答案时,WMD 和 TextAreaResizer 是两个独立的插件,它们都不是由 SO 开发团队编写的(另请参阅:micahwittman 的回答)。 此外,该网站的 JavaScript 非常容易阅读...这些都不再是严格的真实情况,但 TextAreaResizer 仍然工作得很好。

StackOverflow uses a jQuery plugin to accomplish this: TextAreaResizer.

It's easy enough to verify this - just pull the JS files from the site.

Historical note: when this answer was originally written, WMD and TextAreaResizer were two separate plugins, neither one of which was authored by the SO Dev Team (see also: micahwittman's answer). Also, the JavaScript for the site was quite easy to read... None of these are strictly true anymore, but TextAreaResizer still works just fine.

帥小哥 2024-07-13 00:24:41

现在可以使用仅 CSS 的解决方案

(至少需要 Chrome v123

使用(新)field-sizing 属性:

textarea {
  field-sizing: content;
  width: 250px;
}
<textarea>
Here is a
Multiline
Textarea
</textarea>

CSS-only solution is now possible

(requires Chrome v123 minimum)

Using the (new) field-sizing property:

textarea {
  field-sizing: content;
  width: 250px;
}
<textarea>
Here is a
Multiline
Textarea
</textarea>

策马西风 2024-07-13 00:24:41

使用 AngularJS:

angular.module('app').directive('textarea', function() {
  return {
    restrict: 'E',
    controller: function($scope, $element) {
      $element.css('overflow-y','hidden');
      $element.css('resize','none');
      resetHeight();
      adjustHeight();

      function resetHeight() {
        $element.css('height', 0 + 'px');
      }

      function adjustHeight() {
        var height = angular.element($element)[0]
          .scrollHeight + 1;
        $element.css('height', height + 'px');
        $element.css('max-height', height + 'px');
      }

      function keyPress(event) {
        // this handles backspace and delete
        if (_.contains([8, 46], event.keyCode)) {
          resetHeight();
        }
        adjustHeight();
      }

      $element.bind('keyup change blur', keyPress);

    }
  };
});

这将改变所有文本区域以增大/缩小。 如果您只想放大/缩小特定的文本区域 - 将顶部部分更改为如下所示:

angular.module('app').directive('expandingTextarea', function() {
  return {
    restrict: 'A',

希望有帮助!

Using AngularJS:

angular.module('app').directive('textarea', function() {
  return {
    restrict: 'E',
    controller: function($scope, $element) {
      $element.css('overflow-y','hidden');
      $element.css('resize','none');
      resetHeight();
      adjustHeight();

      function resetHeight() {
        $element.css('height', 0 + 'px');
      }

      function adjustHeight() {
        var height = angular.element($element)[0]
          .scrollHeight + 1;
        $element.css('height', height + 'px');
        $element.css('max-height', height + 'px');
      }

      function keyPress(event) {
        // this handles backspace and delete
        if (_.contains([8, 46], event.keyCode)) {
          resetHeight();
        }
        adjustHeight();
      }

      $element.bind('keyup change blur', keyPress);

    }
  };
});

This will transform all your textareas to grow/shrink. If you want only specific textareas to grow/shrink - change the top part to read like this:

angular.module('app').directive('expandingTextarea', function() {
  return {
    restrict: 'A',

Hope that helps!

甜点 2024-07-13 00:24:41

这个怎么样,它的工作

<textarea oninput='this.style.height = "";this.style.height = this.scrollHeight + "px"'></textarea>

what about this, its work

<textarea oninput='this.style.height = "";this.style.height = this.scrollHeight + "px"'></textarea>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文