jquery----TextArea高度自适应

来源:互联网 发布:windows tracert 实现 编辑:程序博客网 时间:2024/06/04 14:34
//最小高度和最大高度默认
02  $("#textarea1").textareaAutoHeight();
03  //最大高度为100px
04  $("#textarea2").textareaAutoHeight({ maxHeight:100 });
05  //最小高度为50px,最大高度为200px
06  $("#textarea3").textareaAutoHeight({ minHeight:50, maxHeight:200 });
07 
08$.fn.extend({
09    textareaAutoHeight:function (options) {
10        this._options = {
11            minHeight: 0,
12            maxHeight: 1000
13        }
14 
15        this.init =function () {
16            for(var p in options) {
17                this._options[p] = options[p];
18            }
19            if(this._options.minHeight == 0) {
20                this._options.minHeight=parseFloat($(this).height());
21            }
22            for(var p in this._options) {
23                if($(this).attr(p) ==null) {
24                    $(this).attr(p,this._options[p]);
25                }
26            }
27            $(this).keyup(this.resetHeight).change(this.resetHeight)
28            .focus(this.resetHeight);
29        }
30        this.resetHeight =function () {
31            var_minHeight = parseFloat($(this).attr("minHeight"));
32            var_maxHeight = parseFloat($(this).attr("maxHeight"));
33 
34            if(!$.browser.msie) {
35                $(this).height(0);
36            }
37            varh = parseFloat(this.scrollHeight);
38            h = h < _minHeight ? _minHeight :
39                        h > _maxHeight ? _maxHeight : h;
40            $(this).height(h).scrollTop(h);
41            if(h >= _maxHeight) {
42                $(this).css("overflow-y","scroll");
43            }
44            else{
45                $(this).css("overflow-y","hidden");
46            }
47        }
48        this.init();
49    }
50});
0 0
原创粉丝点击