在 bootstrap 中自动调整文本区域的大小
Automatically resizing textarea in bootstrap
我在这个 jsfiddle http://jsfiddle.net/tovic/gLhCk/ 中找到了以下非常简单的方法来自动将文本区域调整为文本高度 http://jsfiddle.net/tovic/gLhCk/:
function expandTextarea(id) {
document.getElementById(id).addEventListener('keyup', function() {
this.style.overflow = 'hidden';
this.style.height = 0;
this.style.height = this.scrollHeight + 'px';
}, false);
}
expandTextarea('txtarea');
body {
padding:50px;
}
textarea {
margin:0px 0px;
padding:5px;
height:16px;
line-height:16px;
width:96%;
display:block;
margin:0px auto;
}
<textarea id="txtarea" spellcheck="false" placeholder="Statusku..."></textarea>
但是,我在我的项目中使用 bootstrap,这会带来问题。
- 文本区域只有 4 像素高,而不是 16 像素。似乎 bootstrap 改变了边距、填充和高度的工作方式?
- 回车时,文字上下跳动,刺眼。
我试图在浏览器的 HTML 检查器中删除所有 bootstrap 类,但问题仍然存在。有谁知道如何解决这个问题?
这是添加bootstrap的CSS时的代码:
function expandTextarea(id) {
document.getElementById(id).addEventListener('keyup', function() {
this.style.overflow = 'hidden';
this.style.height = 0;
this.style.height = this.scrollHeight + 'px';
}, false);
}
expandTextarea('txtarea');
body {
padding:50px;
}
textarea {
margin:0px 0px;
padding:5px;
height:16px;
line-height:16px;
width:96%;
display:block;
margin:0px auto;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<textarea id="txtarea" spellcheck="false" placeholder="Statusku..."></textarea>
您可以使用 !important 覆盖 bootstrap 属性。
textarea {
margin:0px 0px;
padding:5px;
height:16px !important;
line-height:16px;
width:96%;
display:block;
margin:0px auto;
}
将height
替换为min-height
textarea {
margin:0px 0px;
padding:5px;
min-height:16px;
line-height:16px;
width:96%;
display:block;
margin:0px auto;
}
感谢您的所有回答,它让我对必须更改的内容有了宝贵的见解。最后,在 css 中多一点 padding-bottom 就成功了。
function expandTextarea(id) {
document.getElementById(id).addEventListener('keyup', function() {
this.style.overflow = 'hidden';
this.style.height = 0;
this.style.height = this.scrollHeight + 'px';
}, false);
}
expandTextarea('txtarea');
body {
padding:50px;
}
textarea {
/* margin:0px 0px; this is redundant anyways since its specified below*/
padding-top:10px;
padding-bottom:25px; /* increased! */
/* height:16px; */
/* line-height:16px; */
width:100%; /* changed from 96 to 100% */
display:block;
/* margin:0px auto; not needed since i have width 100% now */
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<textarea id="txtarea" spellcheck="false" placeholder="Statusku..."></textarea>
我也有同样的问题,bootstrap textarea with autosize 直到我在这里找到解决方案,textarea-autosize lib by javierjulio,它对我有用,你可以试试。
textarea.textarea-autosize {
height: 2.25rem;
min-height: 2.25rem;
resize: none;
overflow-y:hidden;
}
textarea.textarea-autosize.form-control-lg {
height: 3.75rem;
min-height: 3.75rem;
}
textarea.textarea-autosize.form-control-sm {
height: 2rem;
min-height: 2rem;
}
有关此库的示例,您可以访问此页面 jsfiddle。谢谢
如果想让输入框自动适应文字高度。您可以尝试不使用 input / textarea 而是使用 div 这样的:
<div contenteditable="true" aria-multiline="true"></div>
getbootstrap.com 说:
<div class="overflow-auto">...</div>
<div class="overflow-hidden">...</div>
隐藏似乎可以很好地完成工作,即.. 删除溢出。
一如既往,你一定在里面
更简单,只需添加一个属性,如rows="10",你想显示多少就显示多少。
function expandTextarea(id) {
document.getElementById(id).addEventListener('keyup', function() {
this.style.overflow = 'hidden';
this.style.height = 0;
this.style.height = this.scrollHeight + 'px';
}, false);
}
expandTextarea('txtarea');
body {
padding:50px;
}
textarea {
margin:0px 0px;
padding:5px;
height:16px;
line-height:16px;
width:96%;
display:block;
margin:0px auto;
}
<textarea id="txtarea" spellcheck="false" placeholder="Statusku..."></textarea>
我在这个 jsfiddle http://jsfiddle.net/tovic/gLhCk/ 中找到了以下非常简单的方法来自动将文本区域调整为文本高度 http://jsfiddle.net/tovic/gLhCk/:
function expandTextarea(id) {
document.getElementById(id).addEventListener('keyup', function() {
this.style.overflow = 'hidden';
this.style.height = 0;
this.style.height = this.scrollHeight + 'px';
}, false);
}
expandTextarea('txtarea');
body {
padding:50px;
}
textarea {
margin:0px 0px;
padding:5px;
height:16px;
line-height:16px;
width:96%;
display:block;
margin:0px auto;
}
<textarea id="txtarea" spellcheck="false" placeholder="Statusku..."></textarea>
但是,我在我的项目中使用 bootstrap,这会带来问题。
- 文本区域只有 4 像素高,而不是 16 像素。似乎 bootstrap 改变了边距、填充和高度的工作方式?
- 回车时,文字上下跳动,刺眼。
我试图在浏览器的 HTML 检查器中删除所有 bootstrap 类,但问题仍然存在。有谁知道如何解决这个问题?
这是添加bootstrap的CSS时的代码:
function expandTextarea(id) {
document.getElementById(id).addEventListener('keyup', function() {
this.style.overflow = 'hidden';
this.style.height = 0;
this.style.height = this.scrollHeight + 'px';
}, false);
}
expandTextarea('txtarea');
body {
padding:50px;
}
textarea {
margin:0px 0px;
padding:5px;
height:16px;
line-height:16px;
width:96%;
display:block;
margin:0px auto;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<textarea id="txtarea" spellcheck="false" placeholder="Statusku..."></textarea>
您可以使用 !important 覆盖 bootstrap 属性。
textarea {
margin:0px 0px;
padding:5px;
height:16px !important;
line-height:16px;
width:96%;
display:block;
margin:0px auto;
}
将height
替换为min-height
textarea {
margin:0px 0px;
padding:5px;
min-height:16px;
line-height:16px;
width:96%;
display:block;
margin:0px auto;
}
感谢您的所有回答,它让我对必须更改的内容有了宝贵的见解。最后,在 css 中多一点 padding-bottom 就成功了。
function expandTextarea(id) {
document.getElementById(id).addEventListener('keyup', function() {
this.style.overflow = 'hidden';
this.style.height = 0;
this.style.height = this.scrollHeight + 'px';
}, false);
}
expandTextarea('txtarea');
body {
padding:50px;
}
textarea {
/* margin:0px 0px; this is redundant anyways since its specified below*/
padding-top:10px;
padding-bottom:25px; /* increased! */
/* height:16px; */
/* line-height:16px; */
width:100%; /* changed from 96 to 100% */
display:block;
/* margin:0px auto; not needed since i have width 100% now */
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<textarea id="txtarea" spellcheck="false" placeholder="Statusku..."></textarea>
我也有同样的问题,bootstrap textarea with autosize 直到我在这里找到解决方案,textarea-autosize lib by javierjulio,它对我有用,你可以试试。
textarea.textarea-autosize {
height: 2.25rem;
min-height: 2.25rem;
resize: none;
overflow-y:hidden;
}
textarea.textarea-autosize.form-control-lg {
height: 3.75rem;
min-height: 3.75rem;
}
textarea.textarea-autosize.form-control-sm {
height: 2rem;
min-height: 2rem;
}
有关此库的示例,您可以访问此页面 jsfiddle。谢谢
如果想让输入框自动适应文字高度。您可以尝试不使用 input / textarea 而是使用 div 这样的:
<div contenteditable="true" aria-multiline="true"></div>
getbootstrap.com 说:
<div class="overflow-auto">...</div>
<div class="overflow-hidden">...</div>
隐藏似乎可以很好地完成工作,即.. 删除溢出。 一如既往,你一定在里面
更简单,只需添加一个属性,如rows="10",你想显示多少就显示多少。
function expandTextarea(id) {
document.getElementById(id).addEventListener('keyup', function() {
this.style.overflow = 'hidden';
this.style.height = 0;
this.style.height = this.scrollHeight + 'px';
}, false);
}
expandTextarea('txtarea');
body {
padding:50px;
}
textarea {
margin:0px 0px;
padding:5px;
height:16px;
line-height:16px;
width:96%;
display:block;
margin:0px auto;
}
<textarea id="txtarea" spellcheck="false" placeholder="Statusku..."></textarea>