jquery 控制textArea 随文本增加而变高

来源:互联网 发布:淘宝商城布艺沙发 编辑:程序博客网 时间:2024/05/16 00:48
$.fn.autoTextarea = function (options) {             var defaults = {                 maxHeight: null,//文本框是否自动撑高,默认: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.maxHeight && this.scrollHeight > opts.maxHeight) {                             height = opts.maxHeight;                             style.overflowY = 'scroll';                         } else {                             height = this.scrollHeight;                             style.overflowY = 'hidden';                         }                         style.height = height + 'px';                     }                 });             });         }; 


 

 

$.fn.extend({             textareaAutoHeight: function (options) {                 this._options = {                     minHeight: 0,                     maxHeight: 1000                 }                   this.init = function () {                     for (var p in options) {                         this._options[p] = options[p];                     }                     if (this._options.minHeight == 0) {                         this._options.minHeight = parseFloat($(this).height());                     }                     for (var p in this._options) {                         if ($(this).attr(p) == null) {                             $(this).attr(p, this._options[p]);                         }                     }                     $(this).keyup(this.resetHeight).change(this.resetHeight)                     .focus(this.resetHeight);                 }                 this.resetHeight = function () {                     var _minHeight = parseFloat($(this).attr("minHeight"));                     var _maxHeight = parseFloat($(this).attr("maxHeight"));                       if (!window.attachEvent) {                         $(this).height(0);                     }                     var h = parseFloat(this.scrollHeight);                     h = h < _minHeight ? _minHeight :                                 h > _maxHeight ? _maxHeight : h;                     $(this).height(h).scrollTop(h);                     if (h >= _maxHeight) {                         $(this).css("overflow-y", "scroll");                     }                     else {                         $(this).css("overflow-y", "hidden");                     }                 }                 this.init();             }         }); 


 

0 0
原创粉丝点击