01.
window.addEventListener?window.addEventListener(
"load"
,createGradient,
false
):window.attachEvent(
"onload"
,createGradient);
02.
03.
function
createGradient() {
04.
if
(!document.getElementById)
return
;
05.
06.
objArray = getGradientObjects();
07.
if
(!objArray.length)
return
;
08.
09.
for
(i=0;i<objArray.length;i++) {
10.
params = objArray[i].className.split(
" "
);
11.
if
(document.all && !window.opera) {
12.
objArray[i].style.width = objArray[i].offsetWidth +
"px"
;
13.
params[3] ==
"horizontal"
?gType = 1:gType = 0;
14.
objArray[i].style.filter =
"progid:DXImageTransform.Microsoft.Gradient(GradientType="
+gType+
",StartColorStr=\"#"
+ params[1] +
"\",EndColorStr=\"#"
+ params[2] +
"\")"
;
15.
}
else
{
16.
colorArray = createColorPath(params[1],params[2]);
17.
x=0;y=0;
18.
if
(params[3] ==
"horizontal"
) {
19.
w=Math.round(objArray[i].offsetWidth/colorArray.length);
20.
if
(!w)w=1;
21.
h = objArray[i].offsetHeight;
22.
}
else
{
23.
h = Math.round(objArray[i].offsetHeight/colorArray.length);
24.
if
(!h) h =1;
25.
w = objArray[i].offsetWidth;
26.
}
27.
makeGrandParent(objArray[i]);
28.
tmpDOM = document.createDocumentFragment();
29.
for
(p=0;p<colorArray.length;p++) {
30.
g = document.createElement(
"div"
);
31.
g.setAttribute(
"style"
,
"position:absolute;z-index:0;top:"
+ y +
"px;left:"
+ x +
"px;height:"
+ h +
"px;width:"
+ w +
"px;background-color:rgb("
+ colorArray[p][0] +
","
+ colorArray[p][1] +
","
+ colorArray[p][2] +
");"
);
32.
params[3] ==
"horizontal"
?x+=w:y+=h;
33.
tmpDOM.appendChild(g);
34.
if
(y>=objArray[i].offsetHeight || x >= objArray[i].offsetWidth)
break
;
35.
}
36.
objArray[i].appendChild(tmpDOM);
37.
tmpDOM =
null
;
38.
}
39.
}
40.
}
41.
42.
function
getGradientObjects() {
43.
a = document.getElementsByTagName(
"*"
);
44.
objs =
new
Array();
45.
for
(i=0;i<a.length;i++) {
46.
c = a[i].className;
47.
if
(c !=
""
)
if
(c.indexOf(
"gradient"
) == 0) objs[objs.length] = a[i];
48.
}
49.
return
objs;
50.
}
51.
52.
function
createColorPath(color1,color2) {
53.
colorPath =
new
Array();
54.
colorPercent = 1.0;
55.
do
{
56.
colorPath[colorPath.length]=setColorHue(longHexToDec(color1),colorPercent,longHexToDec(color2));
57.
colorPercent-=.01;
58.
}
while
(colorPercent>0);
59.
return
colorPath;
60.
}
61.
62.
function
setColorHue(originColor,opacityPercent,maskRGB) {
63.
returnColor=
new
Array();
64.
for
(w=0;w<originColor.length;w++) returnColor[w] = Math.round(originColor[w]*opacityPercent) + Math.round(maskRGB[w]*(1.0-opacityPercent));
65.
return
returnColor;
66.
}
67.
68.
function
longHexToDec(longHex) {
69.
return
new
Array(toDec(longHex.substring(0,2)),toDec(longHex.substring(2,4)),toDec(longHex.substring(4,6)));
70.
}
71.
72.
function
toDec(hex) {
73.
return
parseInt(hex,16);
74.
}
75.
76.
function
makeGrandParent(obj) {
77.
disp = document.defaultView.getComputedStyle(obj,
''
).display;
78.
disp ==
"block"
?nSpan = document.createElement(
"div"
): nSpan = document.createElement(
"span"
);
79.
mHTML = obj.innerHTML;
80.
obj.innerHTML =
""
;
81.
nSpan.innerHTML = mHTML;
82.
nSpan.setAttribute(
"style"
,
"position:relative;z-index:10;"
);
83.
obj.appendChild(nSpan);
84.
}