forked from SamWM/jQuery-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.focusfields.js
125 lines (122 loc) · 3.36 KB
/
jquery.focusfields.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
/**
*
* Copyright (c) 2007, 2009 Sam Collett (http://www.texotela.co.uk)
* Licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
* Version 2.0
* Demo: http://www.texotela.co.uk/code/jquery/focusfields/
*/
(function($) {
/**
* Outlines input elements (of type text) and/or textarea's when they gain focus
*
* @name focusFields
* @param oColour Outline colour
* @param oWidth Outline width
* @param bgColour Background colour
* @param textColour Text Colour
*/
$.fn.focusFields = $.fn.focusfields = function(oColour, oWidth, bgColour, textColour)
{
this.each(
function()
{
// if node is not an input or textarea, skip
if((this.nodeName.toLowerCase() != "input"
&& this.nodeName.toLowerCase() != "textarea")
// also skip the following types of inputs:
|| this.type == "checkbox"
|| this.type == "radio"
|| this.type == "image"
|| this.type == "reset"
|| this.type == "submit")
{
return;
}
oColour = oColour || "#9cc";
oWidth = oWidth || 2;
var $this = $(this);
$this.data("oldbgcolor", $this.css("background-color") || "#fff")
.data("oldtextcolour", $this.css("color") || "#000");
bgColour = bgColour || this.oldbgcolour;
textColour = textColour || this.oldtextcolour;
var isIE = false;
/*@cc_on
isIE = true;
@*/
if(isIE)
{
var outlineElement = document.createElement("span");
outlineElement.className = "outline";
$this.data("borderCssOff", {
"background-color": $this.parent().css("background-color") || "#fff",
"padding": oWidth + "px"
})
.data("borderCssOn", { "background-color": oColour })
.focus(
function()
{
$(this.parentNode).css($this.data("borderCssOn"));
$(this).css({backgroundColor: bgColour, color: textColour});
}
)
.blur(
function()
{
$(this.parentNode).css($this.data("borderCssOff"));
$(this).css({backgroundColor: $this.data("oldbgcolor"), color: $this.data("oldtextcolour")});
}
);
$(outlineElement).css($this.data("borderCssOff"));
// remove existing wrapper if reapplied
if($this.parent()[0].className == "outline")
{
var $parent = $this.parent();
$parent.after($this);
$parent.parent()[0].removeChild($parent[0]);
}
$this.wrap(outlineElement);
}
else
{
// apply a margin equal to the width of the outline (to prevent overlap)
$this.css({margin: oWidth + "px"})
.data("outlineCssOff", {
"outline-style": "solid",
"outline-width": oWidth + "px"
})
.data("outlineCssOn", {
"outline-color": oColour
});
var $parent = $this.parent(), parentBG;
do
{
parentBG = $parent.css("background-color") || "#fff";
$parent = $parent.parent();
if($parent[0] == document) break;
}
while (parentBG == "transparent")
if(parentBG == "transparent") parentBG = "#fff";
$this.data("outlineCssOff", {
"outline-color": parentBG
})
.css($this.data("outlineCssOff"))
.focus(
function()
{
$(this).css($this.data("outlineCssOn")).css({backgroundColor: bgColour, color: textColour});
}
)
.blur(
function()
{
$(this).css($this.data("outlineCssOff")).css({backgroundColor: $this.data("oldbgcolour"), color: $this.data("oldtextcolour")});
}
);
}
}
);
return this;
}
})(jQuery);