如何建立一个自动高度文本区域
how to build a auto height textarea
我想让textarea的高度等于其中文本的高度(即使在第一次渲染时调整大小并按回车键确认)…
该页面的代码如下所示。如有任何帮助或指点,我将不胜感激。
= = =
resizeTextarea.js
app.directive('resizeTextarea', function() {
return {
restrict: 'E',
transclude: true,
replace: true,
template: "<textarea placeholder='please fill in...'></textarea>",
link: function(scope, element, attrs) {
var HEIGHT = 25;
var el = angular.element(element[0])
el.css('lineHeight', HEIGHT + "px");
el.css('height', HEIGHT + "px");
var resize = function(e) {
var textHeight = e.target.scrollHeight;
var height = ~~(textHeight / HEIGHT) * HEIGHT
el.css('height', height + "px");
};
el.on('input', resize);
}
}
});
= = =
index . html
<div>
<resize-textarea></resize-textarea>
</div>
resize()
。使用scrollHeight
获取textarea的滚动高度
var resize = function (e) {
el.css({
'height': 'auto',
'height': this.scrollHeight + 'px' // Get the scroll height of textarea
});
};
el.on('input', resize);
Thanks to https://stackoverflow.com/a/5346855/2025923
希望它能起作用。检查一下jsfiddle。文本区域将自动增长,并在限制(高度)后,它将显示滚动。当你按退格键或删除所有文本时,它会缩小到原来的大小。
chekc this following link:
https://jsfiddle.net/xwkw3a2r/1/我将使用几行代码创建一个自动调整大小的文本区域:
$(document).ready(function () {
$('textarea').keypress(function () {
var scroll = $('textarea').scrollTop();
if (scroll > 0) {
$('textarea').height($('textarea').height() + scroll);
}
});
});
JSFiddle demo here
试试这个…这将有助于…ok
var app = angular.module('myApp', []);
app.controller('AppCtrl', ['$scope', '$http', '$timeout',
function($scope, $http, $timeout) {
// Load the data
$http.get('http://www.corsproxy.com/loripsum.net/api/plaintext').then(function(res) {
$scope.loremIpsum = res.data;
$timeout(expand, 0);
});
$scope.autoExpand = function(e) {
var element = typeof e === 'object' ? e.target : document.getElementById(e);
var scrollHeight = element.scrollHeight - 60; // replace 60 by the sum of padding-top and padding-bottom
element.style.height = scrollHeight + "px";
};
function expand() {
$scope.autoExpand('TextArea');
}
}
]);
body {
background: #43434B;
padding-top: 100px;
}
textarea {
height: auto;
max-width: 600px;
color: #999;
font-weight: 400;
font-size: 30px;
font-family: 'Ubuntu', Helvetica, Arial, sans-serif;
width: 100%;
background: #fff;
border-radius: 3px;
line-height: 2em;
border: none;
box-shadow: 0px 0px 5px 1px rgba(0, 0, 0, 0.1);
padding: 30px;
-webkit-transition: height 2s ease;
-moz-transition: height 2s ease;
-ms-transition: height 2s ease;
-o-transition: height 2s ease;
transition: height 2s ease;
}
* {
-webkit-font-smoothing: antialiased !important;
}
<link href='http://fonts.googleapis.com/css?family=Ubuntu:300,400' rel='stylesheet' type='text/css'>
<div ng-app="myApp">
<div ng-controller="AppCtrl" align="center">
<textarea id="TextArea" ng-model="loremIpsum" ng-keyup="autoExpand($event)" placeholder="This is an auto expanding textarea with just angularjs ... try typing something.">
</textarea>
</div>
</div>
参考:http://codepen.io/kpourdeilami/pen/KDepk
相关文章:
- onkeyup无法动态创建多个文本区域
- 具有所有样式的文本正在复制到可编辑文本区域
- 在文本区域POST后解码JSON
- 在文本区域中使用jQuery.text()保持换行符
- ng在下拉列表和文本区域提交
- 将文本插入光标所在的文本区域
- 离开页面导航后保留文本区域内容
- Javascript-在文本区域中断,但不在段落中中断
- 当选择值x时,Javascript需要在正确的位置显示文本区域,从而循环通过具有选择选项的表单
- 测试文本区域中的特定文本格式
- 文本编辑后,append函数不适用于文本区域
- 如何在ReactJs中链接下拉列表和文本区域
- I'我试图在文本区域中进行特定的输入,调用特定的javascript函数,但没有成功;不起作用
- 使用JSON文件中的变量(字符串)填充文本区域
- JavaScript-如果以前不存在文本,如何从文本区域删除新行
- 如何在php中创建一个函数,该函数与文本区域一起工作,通过输入类似[color:red]的内容来打印具有等效颜色的文本
- 如何为动态创建的文本区域中输入的值更新ng模型
- 延迟高亮显示文本区域中的文本
- 将字母添加到文本区域的末尾
- TinyMCE在新添加的文本区域