62.textarea 自适应高度

//需要jQuery

  <textarea id="test"></textarea>
    <script src="./libs/jquery.min.js"></script>
    <script>
        (function ($) {
            $.fn.autotextarea = function (options) {
                var defaults = {
                    maxHeight: null,
                    minHeight: $(this).height()
                }
                var opts = $.extend({}, defaults, options)
                return $(this).each(function () {
                    $(this).bind('paste cut keydown keyup focus blur', function () {
                        var height, style = this.style;
                        this.style.height = opts.minHeight + "px";
                        if (this.scrollHeight > opts.minHeight) {
                            if (opts.max && this.scrollHeight > opts.maxHeight) {
                                height = opts.maxHeight;
                                style.oveflowY = "scroll";
                            } else {
                                height = this.scrollHeight;
                                style.oveflowY = "hidden";
                            }
                            style.height = height + "px"
                        } else {

                        }
                    })
                })
            }
        })(jQuery)
    </script>
    <script>
        $("#test").autotextarea({
            maxHeight: 300,
            minHeight: 40,
        })
    </script>

  

猜你喜欢

转载自www.cnblogs.com/famLiu/p/10872292.html
今日推荐