forked from s9y/additional_plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.elastic.js-min
40 lines (35 loc) · 1.5 KB
/
jquery.elastic.js-min
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(function($){
$.fn.extend({
elastic: function() {
var mimics = new Array('paddingTop','paddingRight','paddingBottom','paddingLeft','fontSize','lineHeight','fontFamily','width','fontWeight');
return this.each(function() {
if(this.type == 'textarea') {
var textarea = $(this);
var marginbottom = parseInt(textarea.css('lineHeight'))*2 || parseInt(textarea.css('fontSize'))*2;
var minheight = parseInt(textarea.css('height')) || marginbottom;
var goalheight = 0;
var twin = null;
function update() {
if (!twin)
{
twin = $('<div />').css({'display': 'none','position': 'absolute'}).appendTo('body');
$.each(mimics, function(){
twin.css(this,textarea.css(this));
});
}
var content = textarea.val().replace(/<|>/g, ' ').replace(/\n/g, '<br />').replace(/&/g,"&").replace(/ /g,' ')
if (twin.text() != content)
{
twin.html(content);
goalheight = (twin.height()+marginbottom > minheight)?twin.height()+marginbottom:minheight;
if(Math.abs(goalheight - textarea.height()) > 10)
textarea.animate({'height': goalheight},500);
}
}
textarea.css({overflow: 'hidden',display: 'block'}).bind('focus',function() { self.periodicalUpdater = window.setInterval(function() {update();}, 400); }).bind('blur', function() { clearInterval(self.periodicalUpdater); });
update();
}
});
}
});
})(jQuery);