textarea默认rows为1,只是一行,随内容增加,最大rows为n,怎么实现
style.css
.flex-text-wrap {
position: relative;
*zoom: 1;
}
.wrap {
width: 300px;
height: 300px;
}
textarea,
.flex-text-wrap {
outline: 0;
margin: 0;
border: none;
padding: 0;
*padding-bottom: 0 !important;
}
textarea {
overflow: auto;
vertical-align: top;
resize: vertical;
}
.flex-text-wrap textarea,
.flex-text-wrap pre {
white-space: pre-wrap;
width: 100%;
box-sizing: border-box;
*white-space: pre;
*word-wrap: break-word;
word-wrap: break-word;
}
.flex-text-wrap textarea {
overflow: hidden;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
resize: none;
*height: 94%;
*width: 94%;
}
.flex-text-wrap pre {
display: block;
visibility: hidden;
}
textarea,
.flex-text-wrap pre {
background-clip: padding-box;
transition-duration: 300ms;
transition-easing: ease-in-out;
transition-property: border-color, box-shadow;
padding: 10px;
}
html
<!doctype html>
<html lang="en-gb">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="style.css">
<style>
*{
margin: 0;
padding: 0;
}
#content{
border:1px solid #ddd;
}
</style>
</head>
<body>
<div class="wrap">
<textarea id="content"></textarea>
</div>
<script src="../jquery-2.1.0.js"></script>
<script src="jquery.flexText.js"></script>
<script>
$(function () {
$('#content ').flexText();
});
</script>
</body>
</html>
js
(function ($) {
// Constructor
function FT(elem) {
this.$textarea = $(elem);
this._init();
}
FT.prototype = {
_init: function () {
var _this = this;
this.$textarea.wrap('<div class="flex-text-wrap" />').before('<pre><span /><br /></pre>');
this.$span = this.$textarea.prev().find('span');
this.$textarea.on('input propertychange keyup change', function () {
_this._mirror();
});
$.valHooks.textarea = {
get: function (elem) {
return elem.value.replace(/\r?\n/g, "\r\n");
}
};
this._mirror();
}
,_mirror: function () {
this.$span.text(this.$textarea.val());
}
};
$.fn.flexText = function () {
return this.each(function () {
if (!$.data(this, 'flexText')) {
$.data(this, 'flexText', new FT(this));
}
});
};
})(jQuery);
5 回答1.3k 阅读✓ 已解决
3 回答2.5k 阅读✓ 已解决
3 回答1.4k 阅读✓ 已解决
2 回答1.4k 阅读✓ 已解决
1 回答859 阅读✓ 已解决
5 回答1.1k 阅读
1 回答1.1k 阅读✓ 已解决