forked from s9y/additional_plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.elastic.js-def
65 lines (55 loc) · 2.15 KB
/
jquery.elastic.js-def
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(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 lineheight = parseInt(textarea.css('lineHeight'),10) || parseInt(textarea.css('fontSize'),'10');
var minheight = parseInt(textarea.css('height'),10) || lineheight*3;
var maxheight = parseInt(textarea.css('max-height'),10) || 0;
var goalheight = 0;
var twin = null;
var first = true;
if(maxheight < 0) maxheight = 0;
if (!twin)
{
twin = $('<div />').css({'position': 'absolute','display':'none'}).appendTo(textarea.parent());
$.each(mimics, function(){
twin.css(this.toString(),textarea.css(this.toString()));
});
};
function update() {
var content = textarea.val().replace(/<|>/g, ' ').replace(/\n/g, '<br />').replace(/&/g,"&");
if (twin.text() != content)
{
twin.html(content + " ");
goalheight = (twin.height()+lineheight*2 > minheight)?twin.height()+lineheight*2:minheight;
if(goalheight <= maxheight || maxheight == 0){
textarea.css({overflow: 'hidden'});
if(Math.abs(goalheight - textarea.height()) > lineheight){
textarea.css({'height': goalheight+(lineheight*2) + 'px'});
}
} else {
textarea.css({overflow: 'auto'});
if(maxheight != textarea.height()){
textarea.css({'height': maxheight + 'px'});
if(first){
temp = textarea.val();
textarea.val('');
setTimeout(function() {
textarea.val(temp);
}, 1);
first = false;
}
}
}
}
}
textarea.css({overflow: 'auto'}).bind('focus',function() { self.periodicalUpdater = window.setInterval(function() {update();}, 400); }).bind('blur', function() { clearInterval(self.periodicalUpdater); });
update();
}
});
}
});
})(jQuery);