简体   繁体   English

jQuery textarea调整宽度和高度的大小

[英]jquery textarea resize both width and height

I am trying to create jQuery plugin for both Firefox and Chrome that will make textarea to auto grow for both height and width. 我正在尝试为Firefox和Chrome创建jQuery插件,这将使textarea高度和宽度自动增长。 This is what I have done . 这就是我所做的

HTML: HTML:

<div class="textbox">
    <textarea>Some Text</textarea>
</div>

CSS: 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: 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>&nbsp;');
                if (content === "") {
                    content = "&nbsp;";
                }

                clone.html(content);

                elem.css({
                    height: clone.height(),
                    width: clone.width() + 5
                });
            }
            textarea.on("keyup", adjust);

            setMaxSize();
            adjust();

        });
    };

    $(".textbox").textbox();
});

I have a few problems: 我有几个问题:

  1. Whenever I type a letter and expand the box, only in Firefox I get weird blinking. 每当我输入字母并展开该框时,仅在Firefox中,我都会出现怪异的闪烁。 How can I get rid of that blinking? 我该如何消除闪烁?
  2. In Chrome, when I type a letter, the first letter is moving backward and then return to its position. 在Chrome中,当我键入一个字母时,第一个字母会向后移动,然后返回其位置。 How can I prevent this? 我该如何预防?
  3. In Chrome, whenever I type a long word, "Sdfsdfsdfwejhrkjsdhfsdf" for example, the text doesn't break a new line when reach to max-width, but a new line appears. 例如,在Chrome浏览器中,每当我输入一个长字“ Sdfsdfsdfwejhrkjsdhfsdf”时,文本到达最大宽度时都不会换行,但是会出现一个新行。 How can I fix this behavior? 如何解决此问题?

您可以在.textbox元素上使用CSS过渡,即: transition: all 0.5s;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM