-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.inprogress.js
More file actions
66 lines (60 loc) · 2.2 KB
/
jquery.inprogress.js
File metadata and controls
66 lines (60 loc) · 2.2 KB
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
66
/*
* inProgress - A progress bar for text inputs with limits
*
* MIT License
* http://opensource.org/licenses/MIT
*
* @author : Devin Hayes
* @doc : http://devinhayes.com
* @version : 1.0
*
*/
(function ( $ ) {
$.fn.inprogress = function(options) {
var settings = $.extend({
max: (this.prop('maxlength') ? this.prop('maxlength') : 500),
bgColor: false,
minHeight: '3px',
showPercent: false,
showRemaining: false,
textColor: '#fff',
}, options);
var getColor = function(percent){
var value = (percent/100);
var hue=((1-value)*120).toString(10);
return ["hsl(",hue,",100%,50%)"].join("");
}
var id = 'inprogress'+Math.floor(Math.random()*100000000);
this.after('<div id="'+id+'"><div><span></span></div></div>');
$('#'+id).css('max-width', this.outerWidth());
if(this.width()) {
$(window).resize(function(){
$('#'+id).css('max-width', this.outerWidth());
});
}
$('#'+id+' div').css('width','0%');
this.on('keyup', function(e){
var percent = (($(this).val().length / settings.max) * 100);
$('#'+id).css('max-width', $(this).outerWidth()); // Keep it updated.
if (percent >= 100 && (e.keyCode != 46 && e.keyCode != 8)) {
$(this).val($(this).val().substr(0, settings.max));
e.preventDefault();
}
$('#'+id+' div').animate({
width: percent+'%'
}, 100).css({
'background-color': (settings.bgColor ? settings.bgColor : getColor(percent)),
'min-height': settings.minHeight
});
if(settings.showPercent || settings.showRemaining){
$('#'+id+' div').css('text-align','center');
$('#'+id+' div span')
.css({'color': settings.textColor})
.text((settings.showRemaining ? (settings.max-$(this).val().length) : percent.toFixed()+'%'));
}
});
if(this.val().length){
this.keyup();
}
};
}(jQuery));