我正在尝试为Firefox和Chrome创建jQuery插件,这将使textarea
自动增长高度和宽度。
This is what I have done
HTML:
<div class="textbox">
<textarea>Some Text</textarea>
</div>
CSS:
body, html {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
}
.textbox {
position: absolute;
border: 4px dashed #CCCCCC;
min-width: 30px;
padding: 8px;
}
.textbox textarea {
background: transparent;
padding: 0;
margin: 0;
resize: none;
font-family: Arial, sans-serif;
font-size: 60px;
overflow: hidden;
width: 100%;
height: 100%;
border: none;
white-space: nowrap;
}
.hiddendiv {
display: none;
white-space: pre-wrap;
word-wrap: break-word;
overflow-wrap: break-word;
}
JS:
$(function() {
$.fn.textbox = function(options) {
options = options || {};
var maxWidth = options.maxWidth || $(window).width(),
maxHeight = options.maxHeight || $(window).height();
this.each(function() {
var elem = $(this),
clone = $("<div class='hiddendiv'></div>").appendTo("body"),
textarea = elem.find("textarea"),
content = "";
function setMaxSize() {
var widthSpace = elem.outerWidth(true) - elem.width();
var heightSpace = elem.outerHeight(true) - elem.height();
var newMax = {
"max-width": maxWidth - widthSpace,
"max-height": maxHeight - heightSpace
};
elem.css(newMax);
clone.css(newMax);
}
function adjust() {
clone.css({
"font-family": textarea.css("font-family"),
"font-size": textarea.css("font-size")
});
content = textarea.val().replace(/\n/g, '<br> ');
if (content === "") {
content = " ";
}
clone.html(content);
elem.css({
height: clone.height(),
width: clone.width() + 5
});
}
textarea.on("keyup", adjust);
setMaxSize();
adjust();
});
};
$(".textbox").textbox();
});
我有一些问题:
答案 0 :(得分:0)
您可以在.textbox
元素上使用css转换,即:transition: all 0.5s;