-
Notifications
You must be signed in to change notification settings - Fork 104
/
jquery.label_better.js
160 lines (137 loc) · 5.6 KB
/
jquery.label_better.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* ===========================================================
* jquery-label_better.js v1.1
* ===========================================================
* Copyright 2013 Pete Rojwongsuriya.
* http://www.thepetedesign.com
*
* Label your form input like a boss with
* beautiful animation and without taking up space
*
* https://github.com/peachananr/label_better
*
* ========================================================== */
!function($){
var defaults = {
position: "top",
animationTime: 500,
easing: "ease-in-out",
offset: 20,
hidePlaceholderOnFocus: true
};
$.fn.animateLabel = function(settings, btn) {
var position = btn.data("position") || settings.position,
posx = 0,
posy = 0;
$(this).css({
"left": "auto",
"right": "auto",
"position": "absolute",
"-webkit-transition": "all " + settings.animationTime + "ms " + settings.easing,
"-moz-transition": "all " + settings.animationTime + "ms " + settings.easing,
"-ms-transition": "all " + settings.animationTime + "ms " + settings.easing,
"transition": "all " + settings.animationTime + "ms " + settings.easing
});
switch (position) {
case 'top':
posx = 0;
posy = ($(this).height() + settings.offset) * -1;
$(this).css({
"top": "0",
"opacity": "1",
"-webkit-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
"-moz-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
"-ms-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
"transform": "translate3d(" + posx + ", " + posy + "px, 0)"
});
break;
case 'bottom':
posx = 0;
posy = ($(this).height() + settings.offset);
$(this).css({
"bottom": "0",
"opacity": "1",
"-webkit-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
"-moz-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
"-ms-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
"transform": "translate3d(" + posx + ", " + posy + "px, 0)"
});
break;
case 'left':
posx = ($(this).width() + settings.offset) * -1;
posy = 0;
$(this).css({
"left": 0,
"top": 0,
"opacity": "1",
"-webkit-transform": "translate3d(" + posx + "px, " + posy + "px, 0)",
"-moz-transform": "translate3d(" + posx + "px, " + posy + "px, 0)",
"-ms-transform": "translate3d(" + posx + "px, " + posy + "px, 0)",
"transform": "translate3d(" + posx + "px, " + posy + "px, 0)"
});
break;
case 'right':
posx = $(this).width() + settings.offset;
posy = 0;
$(this).css({
"right": 0,
"top": 0,
"opacity": "1",
"-webkit-transform": "translate3d(" + posx + "px, " + posy + "px, 0)",
"-moz-transform": "translate3d(" + posx + "px, " + posy + "px, 0)",
"-ms-transform": "translate3d(" + posx + "px, " + posy + "px, 0)",
"transform": "translate3d(" + posx + "px, " + posy + "px, 0)"
});
break;
}
}
$.fn.removeAnimate = function(settings, btn) {
var position = btn.data("position") || settings.position,
posx = 0,
posy = 0;
$(this).css({
"top": "0",
"opacity": "0",
"-webkit-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
"-moz-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
"-ms-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
"transform": "translate3d(" + posx + ", " + posy + "px, 0)"
});
}
$.fn.label_better = function(options){
var settings = $.extend({}, defaults, options),
el = $(this),
triggerIn = "focus",
triggerOut = "blur";
if(settings.easing == "bounce") settings.easing = "cubic-bezier(0.175, 0.885, 0.420, 1.310)"
el.each(function( index, value ) {
var btn = $(this),
position = btn.data("position") || settings.position;
btn.wrapAll("<div class='lb_wrap' style='position:relative; display: inline;'></div>")
if( btn.val().length > 0) {
var text = btn.data("new-placeholder") || btn.attr("placeholder");
$("<div class='lb_label " + position + "'>"+ text + "</div>").css("opacity", "0").insertAfter(btn).animateLabel(settings, btn);
}
btn.bind(triggerIn, function() {
if(btn.val().length < 1) {
var text = btn.data("new-placeholder") || btn.attr("placeholder"),
position = btn.data("position") || settings.position;
$("<div class='lb_label " + position + "'>"+ text + "</div>").css("opacity", "0").insertAfter(btn).animateLabel(settings, btn);
}
if (settings.hidePlaceholderOnFocus == true) {
btn.data("default-placeholder", btn.attr("placeholder"))
btn.attr("placeholder", "")
}
btn.parent().find(".lb_label").addClass("active");
}).bind(triggerOut, function() {
if(btn.val().length < 1) {
btn.parent().find(".lb_label").bind("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){ $(this).remove(); }).removeAnimate(settings, btn)
}
if (settings.hidePlaceholderOnFocus == true) {
btn.attr("placeholder", btn.data("default-placeholder"))
btn.data("default-placeholder", "")
}
btn.parent().find(".lb_label").removeClass("active");
});
});
}
}(window.jQuery);