textarea自动增长功能在每次按键时都会抖动

时间:2009-11-07 02:20:44

标签: javascript jquery

我正在使用jquery autogrow插件,它会根据文本需要自动扩展文本的高度。问题是,每按一下键,textarea的底部边框就会以明显的方式抖动。我不确定问题可能是什么,所以我要走出去,在这里发布这个GPL插件的132行。问题所在的任何提示或如何规避它?

         /* 

      Auto Expanding Text Area (1.2.2)  by Chrys Bader */

        (function(jQuery) {
           var self = null;     
           jQuery.fn.autogrow = function(o){
             return this.each(function() {  
                new jQuery.autogrow(this, o);
             });
           };


      /**
      * The autogrow object.
      *
      * @constructor
      * @name jQuery.autogrow
      * @param Object e The textarea to create the autogrow for.
      * @param Hash o A set of key/value pairs to set as configuration properties.
      * @cat Plugins/autogrow
      */        

     jQuery.autogrow = function (e, o) {
        this.options = o || {};         
        this.dummy = null;
        this.interval = null;
        this.line_height = this.options.lineHeight || parseInt(jQuery(e).css('line-height'));
        this.min_height = this.options.minHeight || parseInt(jQuery(e).css('min-height'));
        this.max_height = this.options.maxHeight || parseInt(jQuery(e).css('max-height'));;
        this.textarea = jQuery(e);

       if(this.line_height == NaN) this.line_height = 0;

       // Only one textarea activated at a time, the one being used
       this.init();     
    };  

    jQuery.autogrow.fn = jQuery.autogrow.prototype = { autogrow: '1.2.2' };
    jQuery.autogrow.fn.extend = jQuery.autogrow.extend = jQuery.extend;
    jQuery.autogrow.fn.extend({ init: function(){
       var self = this;
       this.textarea.css({overflow: 'hidden', display: 'block'});
       this.textarea.bind('focus', function(){ self.startExpand() }).bind('blur', function() { self.stopExpand() });
       this.checkExpand();
    },

    startExpand: function() { 
        var self = this;
        this.interval = window.setInterval(function() { self.checkExpand()}, 400); },
    stopExpand: function() { clearInterval(this.interval); },
    checkExpand: function() { 
       if (this.dummy == null) {
          this.dummy = jQuery('<div></div>');
          this.dummy.css({
             'font-size'  : this.textarea.css('font-size'),
             'font-family': this.textarea.css('font-family'),
             'width'      : this.textarea.css('width'),
             'padding'    : this.textarea.css('padding'),
             'line-height': this.line_height + 'px',
             'overflow-x' : 'hidden',
             'position'   : 'absolute',
             'top'        : 0,
             'left'    : -9999
          }).appendTo('body');
      }
// Strip HTML tags          
var html = this.textarea.val().replace(/(<|>)/g,'');
// IE is different, as per usual            
if ($.browser.msie){
    html = html.replace(/\n/g, '<BR>new');
} else {
    html = html.replace(/\n/g, '<br>new');
}

if (this.dummy.html() != html){
   this.dummy.html(html);

if (this.max_height > 0 && (this.dummy.height() + this.line_height > this.max_height)){
    this.textarea.css('overflow-y', 'auto');
} else { 
    this.textarea.css('overflow-y', 'hidden');
    if (this.textarea.height() < this.dummy.height() + this.line_height || (this.dummy.height() < this.textarea.height())) {
    this.textarea.animate({height: (this.dummy.height() + this.line_height) + 'px'}, 100); 
}
}
} 
}
}); 
})(jQuery);

1 个答案:

答案 0 :(得分:1)

关于jeerose的评论:

  

http://www.aclevercookie.com/aclevercookiecom-breached-problem-resolved/

     

它引起了我的注意   访客认为他们的病毒保护   当他们来到这个博客时,他们会离开。   我调查了这件事,发现了   注入了有害代码   进入网站的源头。

     

这已经解决并采取了措施   已被采取增加   网站的安全性。

     

感谢您的举报,我向您道歉   警报。

这似乎不是真的。由于我的防病毒程序在打开该网站时仍然会触发