-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.ieplaceholders.js
103 lines (94 loc) · 2.89 KB
/
jquery.ieplaceholders.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
(function($) {
var prototype = $.fn;
iePlaceholder = prototype.iePlaceholder = function() {
var input = $(this);
// Check normal browsers.
var isOperaMini = Object.prototype.toString.call(window.operamini) == '[object OperaMini]';
var isInputSupported = 'placeholder' in document.createElement('input');
var isTextareaSupported = 'placeholder' in document.createElement('textarea');
// Exit execution for normal browsers.
if (isInputSupported && isTextareaSupported && !isOperaMini) {
return;
}
input.parrent = input.parent();
input.parrent.addClass('placeholder-parrent');
// Prepare placeholder.
var text = input.attr('placeholder');
input.placeholder = $("<div/>", {
'class': 'placeholder-overlay',
'text': text
});
input.placeholder.input = input;
// Copy placeholder styles.
var styles = ['font-size', 'font-weight', 'font-family'];
$.each(styles , function(item, value) {
input.placeholder.css(value, input.css(value));
});
// Calculate placeholder position.
setPosition(input);
input.after(input.placeholder);
// Toggle placeholder.
togglePlaceholder(input);
input.bind('input', function() {
togglePlaceholder(input);
});
input.placeholder.click( function() {
togglePlaceholder(input);
input.focus();
});
// Highlight placeholder on focus.
input.focus(function() {
input.placeholder.addClass('highlighted');
});
// Unhighlight placeholder on blur.
input.blur(function() {
input.placeholder.removeClass('highlighted');
togglePlaceholder(input);
});
/**
* Toggle placeholder.
*/
function togglePlaceholder(input) {
if (input.val() !== '') {
input.placeholder.hide();
}
else {
input.placeholder.show();
}
};
/**
* Calculate input placeholder position.
*
* @returns array position
*/
function setPosition(input) {
var left = 0, top = 0;
if(paddingLeft = parseInt(input.parrent.css('padding-left'))) {
left += paddingLeft;
}
if(margingLeft = parseInt(input.parrent.css('marging-left'))) {
left += paddingLeft;
}
if(paddingLeft = parseInt(input.css('padding-left'))) {
left += paddingLeft;
}
if(margingLeft = parseInt(input.css('marging-left'))) {
left += paddingLeft;
}
if(paddingTop = parseInt(input.parrent.css('padding-top'))) {
top += paddingTop;
}
if(margingTop = parseInt(input.parrent.css('marging-top'))) {
top += margingTop;
}
if(paddingTop = parseInt(input.css('padding-top'))) {
top += paddingTop;
}
if(margingTop = parseInt(input.css('marging-top'))) {
top += margingTop;
}
input.placeholder.css('top', top);
input.placeholder.css('left', left);
};
}
})(jQuery);