forked from jasoncodes/jquery-html5-placeholder-shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.html5-placeholder-shim.js
88 lines (74 loc) · 3.24 KB
/
jquery.html5-placeholder-shim.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
(function($) {
$.fn.placeholderShim = function(method) {
var methods = {
init : function(options) {
var settings = this.placeholderShim.settings = $.extend({}, this.placeholderShim.defaults, options);
return this.each(function() {
var $element = $(this),
element = this;
//if($element.data('placeholder')){
// return true;
//}
var ol = $('<label />');
ol.text($element.attr('placeholder'));
ol.addClass(settings.cls);
ol.css({
position:'absolute',
display: 'inline',
float:'none',
overflow:'hidden',
whiteSpace:'nowrap',
textAlign: 'left',
color: settings.color,
cursor: 'text',
fontSize: parseInt($element.height() * .85)
});
ol.css(helpers.calcPositionCss(element, settings));
ol.data('target', $element);
ol.click(function(){
$(this).data('target').focus()
});
ol.insertAfter($element);
$element
.data('placeholder',ol)
.focus(function(){
ol.hide();
})
.blur(function() {
ol[$element.val().length ? 'hide' : 'show']();
})
.triggerHandler('blur');
$(window).resize(function() {
var $target = ol.data('target');
ol.css(helpers.calcPositionCss($target, settings));
});
});
}
}
var helpers = {
calcPositionCss: function(target, settings)
{
var op = $(target).offsetParent().offset(),
ot = $(target).offset();
return {
top: ot.top - op.top + ($(target).outerHeight() - $(target).height()) /2 + $(target).height()*.07,
left: ot.left - op.left + settings.lr_padding,
width: $(target).width() - settings.lr_padding
};
}
}
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error( 'Method "' + method + '" does not exist in placeholderShim plugin!');
}
}
$.fn.placeholderShim.defaults = {
color: '#888',
cls: 'placeholder_shim',
lr_padding:4
}
$.fn.placeholderShim.settings = {}
})(jQuery);