This commit is contained in:
yumoqing 2026-02-28 14:38:12 +08:00
parent 3829723533
commit fba6f1eee3
2 changed files with 34 additions and 15 deletions

View File

@ -618,18 +618,28 @@ hr {
touch-action: pan-y; touch-action: pan-y;
/* 3. 极其重要:有些 H5 框架会禁用默认滚动,这里强制开启 */ /* 3. 极其重要:有些 H5 框架会禁用默认滚动,这里强制开启 */
pointer-events: auto !important; pointer-events: auto !important;
display: block;
width: 100%; width: 100%;
min-height: 40px; /* 初始高度 */ min-height: 40px; /* 初始高度 */
max-height: 150px; /* 最大高度:超过此高度将滚动 */ max-height: 350px; /* 最大高度:超过此高度将滚动 */
line-height: 1.5; line-height: 1.5;
padding: 10px; padding: 10px;
box-sizing: border-box;
resize: none; /* 禁用右下角手动拉伸 */ resize: none; /* 禁用右下角手动拉伸 */
overflow-y: hidden; /* 初始隐藏滚动条 */ overflow-y: hidden; /* 初始隐藏滚动条 */
overflow-x: hidden;
-webkit-appearance: none; /* 移除 iOS 默认内阴影 */
border: 1px solid #ccc; border: 1px solid #ccc;
border-radius: 4px; border-radius: 4px;
outline: none; outline: none;
transition: border-color 0.2s; transition: none;
}
/* 针对 Chrome/Edge/Safari 的滚动条美化(解决滚动条突然出现导致内容抖动) */
.auto-textarea::-webkit-scrollbar {
width: 5px;
}
.auto-textarea::-webkit-scrollbar-thumb {
background: rgba(0,0,0,0.2);
border-radius: 10px;
} }
.inputbox:focus { .inputbox:focus {

View File

@ -934,26 +934,35 @@ bricks.UiText =class extends bricks.UiType {
this.bind('keydown', this.key_handle.bind(this)); this.bind('keydown', this.key_handle.bind(this));
} }
handleInput() { handleInput() {
// 1. 重置高度为 auto以便能够正确计算缩减后的 scrollHeight // 1. 记录原始滚动位置(防止光标跳动)
var el = this.dom_element; var el = this.dom_element;
el.style.height = 'auto'; const scrollTop = el.scrollTop;
// 2. 获取当前的滚动高度 // 2. 核心:先设为固定高度而非 auto
const scrollHeight = el.scrollHeight; // 避免某些浏览器(如 Safari在 auto 切换时丢失滚动焦点
// 3. 获取 CSS 中定义的 max-height (150px) el.style.height = '0px';
const maxHeight = parseInt(window.getComputedStyle(el).maxHeight);
const paddingTop = parseInt(el.style.paddingTop);
const paddingBottom = parseInt(el.style.paddingBottom);
if (scrollHeight >= maxHeight) { // 3. 获取实时计算样式
// 4. 如果内容高度超过最大高度,固定为最大高度并显示滚动条 const style = window.getComputedStyle(el);
const maxHeight = parseInt(style.maxHeight) || Infinity;
const minHeight = parseInt(style.minHeight) || 40;
// 4. 计算目标高度
// scrollHeight 在所有现代浏览器中都包含 padding
const targetHeight = el.scrollHeight;
if (targetHeight >= maxHeight) {
// 达到或超过最大高度
el.style.height = maxHeight + 'px'; el.style.height = maxHeight + 'px';
el.style.overflowY = 'auto'; el.style.overflowY = 'auto';
} else { } else {
// 5. 否则持续自适应高度,并隐藏滚动条 // 低于最大高度
el.style.height = scrollHeight + 'px'; el.style.height = Math.max(targetHeight, minHeight) + 'px';
el.style.overflowY = 'hidden'; el.style.overflowY = 'hidden';
} }
// 5. 恢复滚动(解决 Chrome/Safari 偶尔的置顶 Bug
el.scrollTop = scrollTop;
} }
create(){ create(){
this.dom_element = this._create('textarea'); this.dom_element = this._create('textarea');