Textarea在添加或删除内容时自动调整大小

时间:2014-02-18 10:03:46

标签: jquery textarea autosize

所以我找到了这段代码,然后按照它

$(function() {
//  changes mouse cursor when highlighting loawer right of box
$("textarea").mousemove(function(e) {
    var myPos = $(this).offset();
    myPos.bottom = $(this).offset().top + $(this).outerHeight();
    myPos.right = $(this).offset().left + $(this).outerWidth();

    if (myPos.bottom > e.pageY && e.pageY > myPos.bottom - 16 && myPos.right > e.pageX && e.pageX > myPos.right - 16) {
        $(this).css({ cursor: "nw-resize" });
    }
    else {
        $(this).css({ cursor: "" });
    }
})
//  the following simple make the textbox "Auto-Expand" as it is typed in
$("textarea").keyup(function(e) {
    //  this if statement checks to see if backspace or delete was pressed, if so, it resets the height of the box so it can be resized properly
    if (e.which == 8 || e.which == 46) {
        $(this).height(parseFloat($(this).css("min-height")) != 0 ? parseFloat($(this).css("min-height")) : parseFloat($(this).css("font-size")));
    }
    //  the following will help the text expand as typing takes place
    while($(this).outerHeight() < this.scrollHeight + parseFloat($(this).css("borderTopWidth")) + parseFloat($(this).css("borderBottomWidth"))) {
        $(this).height($(this).height()+1);
    };
});
});

它适用于自动扩展功能,但它不会随着文本被删除而变小,当我删除内容时它不会变小。有人可以帮我这个吗?

0 个答案:

没有答案