百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
T

textarea-autosize

> 编程语言
开源

根据用户输入,垂直调整文本域,而不使用克隆元素或虚拟元素。

397 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

根据用户输入,垂直调整文本域,而不使用克隆元素或虚拟元素。

textarea-autosize

This package contains a TextareaAutoSize ES6 class for vertically adjusting a textarea based on user input and controlling any presentation in CSS. No clone or ghost elements are used. No jQuery dependency either, just plain vanilla JS. 😉

This was a fun project I started in Aug 2012 as a jQuery plugin. Nowadays, the same hack can be implemented using vanilla JS with such little code that I don't recommend using this package. Instead just implement the component code in your project so it can be modified to your requirements.

Another option is to use CSS only as Chrome and Edge support the field-sizing: content; rule (example)

Installation

NPM

bash
npm install textarea-autosize

Usage

Import the script and find the desired textarea elements on which to initialize the component with.

xml

Minimum CSS Requirements

In the following example, the minimum height is one line of text which is determined from the inherited font size, line height, and vertical padding. So if your font-size is 16px, line-height is 1.5, top/bottom padding is 6px each, then the minimum height would be 36px since 16 * 1.5 + 12 = 36.

css
textarea {
  box-sizing: border-box;
  max-height: 160px; /* optional but recommended */
  min-height: 38px;
}

Increase the min-height to have more initial rows. Once text exceeds that minimum height the textarea will expand naturally until it reaches the maximum height if declared.

Updating Textarea Content

If you pre-fill the textarea before page load the textarea will adjust to fit automatically but if given a value after page load (e.g. single page app) then you will need to trigger an update on the textarea after setting its value for it to size correctly.

javascript
wrapper.update()

Cleaning Up

Calling the destroy method will remove the event handler and release the element reference.

javascript
wrapper.destroy()

Development

  1. Install Node.js v18+
  2. npm install
  3. npx playwright install
  4. npm test
  5. npm run start

Deploy New Release

Replace {type} with any of the following: patch, minor or major

bash
npm version {type}
git push origin --tags
npm run predeploy && npm publish

Update GitHub Project Page

Run npm run deploy to release on GitHub Pages using gh-pages package.

Resources

  • https://github.com/theproductguy/BetterGrow
  • https://github.com/cburgmer/jquery-shiftenter

Issues· 4 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言