<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<html xmlns=
"http://www.w3.org/1999/xhtml"
>
<head>
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
/>
<title>颜色渐变实例</title>
<script type=
"text/javascript"
>
function
$(id){
return
typeof id=='string'?document.getElementById(id):id;
}
function
addEventHandler(oTarget,sEventType,fnHandler){
if
(oTarget.addEventListener){
oTarget.addEventListener(sEventType,fnHandler,false);
}
else
if
(oTarget.attachEvent){
oTarget.attachEvent(
"on"
+sEventType,fnHandler);
}
else
{
oTarget[
"on"
+sEventType]=fnHandler;
}
}
var
Class={
Create:
function
(){
return
function
(){
this.initialize.apply(this,arguments);
}
}
}
Object.extend=
function
(destination,source){
for
(
var
property in source){
destination[property]=source[property];
}
return
destination;
}
var
colorFade=Class.Create();
colorFade.prototype={
initialize:
function
(obj,options){
this._obj=$(obj);
this._timer=null;
this.SetOptions(options);
this.Steps=Math.
abs
(this.options.Steps);
this.Speed=Math.
abs
(this.options.Speed);
this.StartColorArr=this._colorArr=this.getColorArr(this.options.StartColor);
this.EndColorArr=this.getColorArr(this.options.EndColor);
this.Background=this.options.Background;
this._stepAddValueArr=[this.getColorAddValue(this.StartColorArr[0],this.EndColorArr[0]),this.getColorAddValue(this.StartColorArr[1],this.EndColorArr[1]),this.getColorAddValue(this.StartColorArr[2],this.EndColorArr[2])];
this._setObjColor=this.Background?
function
(sColor){
this._obj.style.backgroundColor=sColor;
}:
function
(sColor){
this._obj.style.color=sColor;
};
this._setObjColor(this.options.StartColor);
var
oThis=this;
addEventHandler(this._obj,
"mouseover"
,
function
(){
oThis.Fade(oThis.EndColorArr);
}
);
addEventHandler(this._obj,
"mouseout"
,
function
(){
oThis.Fade(oThis.StartColorArr);
});
},
SetOptions:
function
(options){
this.options={
StartColor:
"#000000"
,
EndColor:
"#ffffff"
,
Steps: 20,
Speed: 20,
Background: true
}
Object.extend(this.options,options||{});
},
getColorArr:
function
(sColor){
var
curColor=sColor.replace(
"#"
,
""
);
var
r,g,b;
if
(curColor.length>3){
r=curColor.
substr
(0,2);
g=curColor.
substr
(2,2);
b=curColor.
substr
(4,2);
}
else
{
r=curColor.
substr
(0,1);
g=curColor.
substr
(1,1);
b=curColor.
substr
(2,1);
r+=r;
g+=g;
b+=b;
}
return
[parseInt(r,16),parseInt(g,16),parseInt(b,16)];
},
getColorAddValue:
function
(sRGB,eRGB){
var
stepValue=Math.
abs
((eRGB-sRGB)/this.Steps);
if
(stepValue>0&&stepValue<1){
stepValue=1;
}
return
parseInt(stepValue);
},
getStepColor:
function
(sColor,eColor,addValue){
if
(sColor==eColor){
return
sColor;
}
else
if
(sColor<eColor){
return
(sColor+addValue)>eColor?eColor:(sColor+addValue);
}
else
if
(sColor>eColor){
return
(sColor-addValue)<eColor?eColor:(sColor-addValue);
}
},
Fade:
function
(endColorArr){
clearTimeout(this._timer);
var
er=endColorArr[0],
eg=endColorArr[1],
eb=endColorArr[2],
r=this.getStepColor(this._colorArr[0],er,this._stepAddValueArr[0]),
g=this.getStepColor(this._colorArr[1],eg,this._stepAddValueArr[1]),
b=this.getStepColor(this._colorArr[2],eb,this._stepAddValueArr[2]);
this._colorArr=[r,g,b];
this._setObjColor(
"#"
+Hex(r) + Hex(g) + Hex(b));
if
(r!=er||g!=eg||b!=eb){
var
oThis=this;
oThis._timer=setTimeout(
function
(){oThis.Fade(endColorArr)},oThis.Speed);
}
}
}
function
Hex(i) {
if
(i < 0)
return
"00"
;
else
if
(i > 255)
return
"ff"
;
else
{
var
str =
"0"
+ i.toString(16);
return
str.substring(str.length - 2);
}
}
</script>
</head>
<body>
<p id=
"test"
style=
"height:40px;width:200px;border:1px solid red;"
>
嘻嘻!
</p>
<p id=
"test1"
style=
"height:40px;width:200px;border:1px solid red;"
>
呵呵!
</p>
<p id=
"test2"
style=
"height:40px;width:200px;border:1px solid red;"
>
哈哈!
</p>
</body>
<script type=
"text/javascript"
>
var
colorFade01=
new
colorFade(
"test"
,{StartColor:'#000000',EndColor:'#8AD4FF',Background:true});
var
colorFade02=
new
colorFade(
"test"
,{StartColor:'#8AD4FF',EndColor:'#000000',Background:false});
var
colorFade03=
new
colorFade(
"test1"
,{StartColor:'#000000',EndColor:'#8AD4FF',Background:true});
var
colorFade04=
new
colorFade(
"test1"
,{StartColor:'#8AD4FF',EndColor:'#000000',Background:false});
var
colorFade05=
new
colorFade(
"test2"
,{StartColor:'#000000',EndColor:'#8AD4FF',Background:true});
var
colorFade06=
new
colorFade(
"test2"
,{StartColor:'#8AD4FF',EndColor:'#000000',Background:false});
</script>
</html>