This repository has been archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloating-label.js
122 lines (112 loc) · 3.29 KB
/
floating-label.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Floating Label
* Version: 1.0
* Author: Thomas Mester
* Repo: https://github.com/hotgeart/floating-label
*/
(function($) {
$.fn.floatingLabel = function(options) {
this.each(function() {
// Default variable
var input = "#" + this.id;
input = $(input);
var container = $(input).parent();
var label = $(input)
.parent()
.children("label");
var notFirstTime = false; // to avoid to play the animation the first time
// Defaults settings
var defaults = {
transition: true,
transitionProperties: "all",
transitionDuration: ".2s",
placeholderColor: false
};
var settings = $.extend({}, defaults, options);
// Setup
container.addClass("floating-label");
container.css("position", "relative");
label.addClass("floating");
style();
// Style
function style(forceFloat) {
// Floating
if (
input.hasClass("not-empty") ||
input.val().length > 0 ||
forceFloat
) {
label.addClass("is");
label.css({
position: "absolute",
top: 0,
left: input.css("padding-left"),
color: input.css("color"),
fontSize: "10px",
lineHeight: "1",
fontStyle: "normal",
textTransform: "uppercase",
backgroundColor: input.css("background-color"),
transform: "translateY(-50%)",
margin: "0 0 0 -3px",
padding: "1px 3px"
});
if (settings.transition && notFirstTime) {
label.css({
"transition-property": settings.transitionProperties,
"transition-duration": settings.transitionDuration
});
}
// NOT floating
} else {
label.removeClass("is");
label.css({
position: "absolute",
top: input.css("padding-top"),
left: input.css("padding-left"),
fontSize: input.css("font-size"),
lineHeight: "",
fontStyle: "italic",
textTransform: "none",
backgroundColor: "transparent",
transform: "none",
margin: 0,
padding: 0
});
if (settings.transition && notFirstTime) {
label.css({
"transition-property": settings.transitionProperties,
"transition-duration": settings.transitionDuration
});
}
if (settings.placeholderColor !== false) {
label.css("color", settings.placeholderColor);
}
}
}
input
.focusin(function() {
notFirstTime = true;
style(true);
})
.focusout(function() {
notFirstTime = true;
style();
});
// Add/remove not-empty class if we're typing content
input.keyup(function() {
if (input.val().length === 0) {
input.removeClass("not-empty");
} else {
input.addClass("not-empty");
}
});
});
// Add not-empty class if the input isn't empty
$("input[type=text]").each(function() {
if ($(this).val().length != 0) {
$(this).addClass("not-empty");
}
});
};
})(jQuery);