-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.floatLabel.js
93 lines (67 loc) · 2.08 KB
/
jquery.floatLabel.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//https://github.com/reubenmoes/jquery.floatLabel
(function($){
'use strict';
var floatLabel = {
defaultOptions: {
activeClass: 'is-active',
noTransitionClass: 'u-no-transition'
},
init: function(element, options){
options = $.extend({}, floatLabel.defaultOptions, options);
$(element).data('floatLabel', options);
floatLabel.bindEvents(element, options);
},
bindEvents: function(element, options){
var $input = $(element).find('input, textarea');
$input.on('focus', function() {
$(element).addClass(options.activeClass);
});
$input.on('blur', function() {
if (!$(this).val().length) {
$(element).removeClass(options.activeClass);
}
});
},
methods: {
//Check to see if a float label class needs to be changed
//Note: we try disable any transitions by adding a class
refresh: function(element){
var $input = $(element).find('input, textarea');
var $label = $(element).find('label');
var options = $(element).data('floatLabel');
$(element).addClass(options.noTransitionClass);
if ($input.is(':focus')) {
$(element).addClass(options.activeClass);
}
if ($input.val() !== '') {
$(element).addClass(options.activeClass);
}
else {
$(element).removeClass(options.activeClass);
}
setTimeout(function(){
$(element).removeClass(options.noTransitionClass);
});
}//refresh()
}//methods{}
};
//Make it a jQuery plugin
$.fn.floatLabel = function(options) {
// Check if we are calling the plugin with a method
// ex $(element).floatLabel('restore');
if ( floatLabel.methods[options] ) {
return this.each(function() {
floatLabel.methods[options](this);
});
}
//Regular plugin init
//$(element).floatLabel()
else {
return this.each(function(index) {
if(!$.data(this, 'floatLabel')){
floatLabel.init(this, options);
}
});
}
};
})(jQuery);