jquery实现textarea 高度自适应

来源:互联网 发布:数据库中select的用法 编辑:程序博客网 时间:2024/05/12 05:49
转载自:http://www.jb51.net/article/61997.htm

用jquery实现的textarea 高度自适应代码。这个动画效果比较流畅。适合新手学习。非常简单实用,这里推荐给小伙伴们。

之前给大家分享过用Javascript控制文本框textarea高度随内容自适应增长收缩,今天花了点时间换了种实现方法,总结一下

复制代码 代码如下:

jQuery.fn.extend({
            autoHeight: function(){
                return this.each(function(){
                    var $this = jQuery(this);
                    if( !$this.attr('_initAdjustHeight') ){
                        $this.attr('_initAdjustHeight', $this.outerHeight());
                    }
                    _adjustH(this).on('input', function(){
                        _adjustH(this);
                    });
                });
                /**
                 * 重置高度
                 * @param {Object} elem
                 */
                function _adjustH(elem){
                    var $obj = jQuery(elem);
                    return $obj.css({height: $obj.attr('_initAdjustHeight'), 'overflow-y': 'hidden'})
                            .height( elem.scrollHeight );
                }
            }
        });
        // 使用
        $(function(){
            $('textarea').autoHeight();
        });
0 0
原创粉丝点击