효과 미리보기 :
<!DOCTYPE HTML>
<html xmlns=" http://www.w3.org/1999/xhtml ">
<머리>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>자바스크립트 색상 그라데이션 및 그라데이션 효과</title>
<스크립트 유형="텍스트/자바스크립트">
eval(함수(p,a,c,k,e,r){e=함수(c){return(c<62?'':e(parseInt(c/62)))+((c=c% 62)>35?String.fromCharCode(c+29):c.toString(36))};if('0'.replace(0,e)==0){while(c--)r[e( c)]=k[c];k=[함수(e){return r[e]||e}];e=함수(){return'([3-59cf-hj-mo-rt-yCG- NP-RT-Z]|[12]\w)'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\b'+ e(c)+'\b','g'),k[c]);return p}('5 $$,$$B,$$A,$$F,$$D,$$E ,$$S;(3(){5 O,B,A,F,D,E,S;O=3(id){4"1L"==1t id?P.getElementById(id):id} ;O.extend=3(G,10){H(5 QR 10){G[Q]=10[Q]}4 G};O.deepextend=3(G,10){H(5 QR 10) {5 17=10[Q];9(G===17)계속;9(1t 17==="c"){G[Q]=I.callee(G[Q]||{},17 )}J{G[Q]=17}}4 G};B=(3(K){5 b={11:/11/.x(K)&&!/1u/.x(K),1u :/1u/.x(K),1M:/webkit/.x(K)&&!/1v/.x(K),1N:/1N/.x(K),1v:/1v/.x( K)};5 1i="";H(5 i R b){9(b[i]){1i="1M"==i?"18":i;19}}b.18=1i&&1w( "(?:"+1i+")[\\/: ]([\\d.]+)").x(K)?1w.$1:"0";b.ie=b. 11;b.1O=b.11&&1y(b.18)==6;b.ie7=b.11&&1y(b.18)==7;b.1P=b.11&&1y(b.18)==8; 4 b})(1z.navigator.userAgent.toLowerCase());A=3(){5 l={isArray:3(1Q){4 Object.1R.toString.L(1Q)==="[c 1S]"},1A:3(C,12,p){9(C.1A){4 C.1A(12)}J{5 M=C.1a;p=1T(p)?0:( p<0)?1j.1U(p)+M:1j.1V(p);H(;p<M;p++){9(C[i]===12)4 i}4-1}} ,1B:3(C,12,p){9(C.1B){4 C.1B(12)}J{5 M=C.1a;p=1T(p)||p>=M-1 ?M-1:p<0?1j.1U(p)+M:1j.1V(p);H(;p>-1;p--){9(C[i]===12)4 i}4-1}}};3 X(c,q){9(정의되지 않음===c.1a){H(5 f R c){9(w===q(c[f],f ,c))19}}J{H(5 i=0,M=c.1a;i<M;i++){9(i R c){9(w===q(c[i],i ,c))19}}}};X({1W:3(c,q,j){XL(j,c,3(){qY(j,I)})},map:3(c, q,j){5 l=[];XL(j,c,3(){l.1X(qY(j,I))});4 l},1k:3(c,q,j){ 5 l=[];XL(j,c,3(1Y){qY(j,I)&&l.1X(1Y)});4 l},every:3(c,q,j){5 l= 1b; =w;XL(j,c,3(){9(qY(j,I)){l=1b;4w}});4l}},3(1Z,f){l[f ]= 3(c,q,j){9(c[f]){4 c[f](q,j)}J{4 1Z(c,q,j)}}});4 l}( ); F=(3(){5 1c=1S.1R.1c;4{바인드:3(1l,j){5 1m=1c.L(I,2);4 3(){4 1l.Y (j ,1m.20(1c.L(I)))}},bindAsEventListener:3(1l,j){5 1m=1c.L(I,2);4 3(g){4 1l.Y( j, [E.1d(g)].20(1m))}}}})();D={1n:3(m){5 13=m?m.21:P;4 13.22.23| |13.24 .23},1o:3(m){5 13=m?m.21:P;4 13.22.25||13.24.25},1C:3(a,b){4(u.1C= a.26?3(a,b){4!!(a.26(b)&16)}:3(a,b){4 a!=b&&a.1C(b)})(a,b)} ,v :3(m){5 o=0,N=0,T=0,U=0;9(!m.27||B.1P){5 n=m;while(n){o+= n.offsetLeft,N+=n.offsetTop;n=n.offsetParent};T=o+m.offsetWidth;U=N+m.offsetHeight}J{5 v=m.27();o=T=u. 1o(m);N=U=u.1n(m);o+=vo;T+=vT;N+=vN;U+=vU};4{"o":o,"N":N,"T" :T ,"U":U}},clientRect:3(m){5 v=uv(m),1D=u.1o(m),1E=u.1n(m);vo-=1D;vT -= 1D;vN-=1E;vU-=1E;4 v},28:3(k){4(u.28=P.1p?3(k){4 P.1p.29(k,2a )} :3(k){4 k.1q})(k)},2b:3(k,f){4(u.2b=P.1p?3(k,f){5h=P. 1p.29(k,2a);4 f R h?h[f]:h.getPropertyValue(f)}:3(k,f){5 h=k.1q;9(f=="Z") {9 (/1F\(Z=(.*)\)/ix(h.1k)){5 Z=parseFloat(1w.$1);4 Z?Z/2c:0}4 1};9 (f ==="2d"){f="2e"}5 l=h[f]||h[S.1G(f)];9(!/^\-?\d+(px) ?$/ ix(l)&&/^\-?\d/.x(l)){h=kh,o=ho,2g=k.1H.o;k.1H.o=k.1q .o; ho=l||0;l=h.pixelLeft+"px";ho=o;k.1H.o=2g}4l})(k,f)},setStyle:3(1e,h, 14){ 9(!1e.1a){1e=[1e]}9(1t h=="1L"){5 s=h;h={};h[s]=14}A.1W(1e ,3( k){H(5 f R h){5 14=h[f];9(f=="Z"&&B.ie){kh1k=(k.1q.1k||"").2h (/1F \([^)]*\)/,"")+"1F(Z="+14*2c+")"}J 9(f=="2d"){kh[B.ie ?"2e ":"cssFloat"]=14}J{kh[S.1G(f)]=14}}})}};E=(3(){5 1f,1g,15=1;9( 1z.2i ){1f=3(r,t,y){r.2i(t,y,w)};1g=3(r,t,y){r.removeEventListener(t,y,w)} }J{ 1f=3(r,t,y){9(!y.$$15)y.$$15=15++;9(!rV)rV={};5 W=rV[t];9 (!W ){W=rV[t]={};9(r["on"+t]){W[0]=r["on"+t]}}W[y.$$15]= y;r ["on"+t]=1r};1g=3(r,t,y){9(rV&&r.V[t]){rV 삭제[t][y.$$15]}};3 1r() {5 1s=1b,g=1d();5 W=uV[gt];H(5 i RW){u.$$1r=W[i];9(u.$$1r(g )== =w){1s=w}}4 1s}}3 1d(g){9(g)4 g;g=1z.g;g.pageX=g.clientX+D.1o();g .pageY= g.clientY+D.1n();g.target=g.srcElement;g.1J=1J;g.1K=1K;switch(gt){2j"mouseout":g.2k=g.toElement ;19; 2j"마우스오버":g.2k=g.fromElement;19};4 g};3 1J(){u.cancelBubble=1b};3 1K(){u.1s=w};4{" 1f": 1f,"1g":1g,"1d":1d}})();S={1G:3(s){4 s.2h(/-([az])/ig,3(all ,2l) {4 2l.toUpperCase()})}};9(B.1O){try{P.execCommand("BackgroundImageCache",w,1b)}catch(e){}};$$=O; $$B =B;$$A=A;$$F=F;$$D=D;$$E=E;$$S=S})();',[],146,'|| |function| return|var||||if||object|||name|event|style||thisp|elem|ret|node||left|from|callback|element||type|this|direct|false| test| 핸들러||||배열|||destination|for|arguments|else|ua|call|len|top||document|property|in||right|bottom|events|handlers|each|apply|opacity|source | msie|elt|doc|value|guid||copy|version|break|length|true|slice|fixEvent|elems|addEvent|removeEvent||vMark|Math|filter|fun|args|getScrollTop|getScrollLeft|defaultView|currentStyle | handlerEvent|returnValue|typeof|opera|chrome|RegExp||parseInt|window|indexOf|lastIndexOf|contains|sLeft|sTop|alpha|camelize|runtimeStyle||stopPropagation|preventDefault|string|safari|firefox|ie6|ie8|obj | 프로토타입|배열|isNaN|ceil|floor|forEach|push|item|method|concat|ownerDocument|documentElement|scrollTop|body|scrollLeft|compareDocumentPosition|getBoundingClientRect|curStyle|getCompulatedStyle|null|getStyle|100|float|styleFloat|| rsLeft |replace|addEventListener|case|관련Target|letter'.split('|'),0,{}))
</script>
<스크립트 유형="텍스트/자바스크립트">
//색상 그라데이션 메소드 가져오기
var ColorGrads = (함수(){
//색상 그라데이션 데이터 가져오기
함수 GetStep(시작, 끝, 단계) {
var colors = [], start = GetColor(start), end = GetColor(end),
stepR = (끝[0] - 시작[0]) / 단계,
stepG = (끝[1] - 시작[1]) / 단계,
stepB = (끝[2] - 시작[2]) / 단계;
//색상 컬렉션 생성
for(var i = 0, r = 시작[0], g = 시작[1], b = 시작[2]; i < 단계; i++){
색상[i] = [r, g, b]; r += 단계R; g += 단계G += 단계B;
}
색상[i] = 끝;
//올바른 색상 값
return $$A.map(colors, function(x){ return $$A.map(x, function(x){
return Math.min(Math.max(0, Math.floor(x)), 255);
});});
}
//색상 데이터 가져오기
var 조각;
함수 GetColor(색상) {
var ret = GetData(색상);
if (ret === 정의되지 않음) {
if (!frag) {
frag = document.createElement("textarea");
frag.style.display = "없음";
document.body.insertBefore(frag, document.body.childNodes[0]);
};
try { frag.style.color = color; } catch(e) { return [0, 0, 0]//즉 오페라
if (document.defaultView) {
//오페라 #rrggbb
ret = GetData(document.defaultView.getCompulatedStyle(frag, null).color);
} 또 다른 {
color = frag.createTextRange().queryCommandValue("ForeColor");
ret = [ 색상 & 0x0000ff, (색상 & 0x00ff00) >>> 8, (색상 & 0xff0000) >>> 16 ];
}
}
반환 ret;
}
//색상 배열 얻기
함수 GetData(색상) {
var re = RegExp;
if (/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.test(color)) {
//#rrggbb
return $$A.map([ re.$1, re.$2, re.$3 ], function(x){
return parInt(x, 16);
});
} else if (/^#([0-9a-f])([0-9a-f])([0-9a-f])$/i.test(color)) {
//#rgb
return $$A.map([ re.$1, re.$2, re.$3 ], function(x){
return parInt(x + x, 16);
});
} else if (/^rgb((.*),(.*),(.*))$/i.test(color)) {
//rgb(n,n,n) 또는 rgb(n%,n%,n%)
return $$A.map([ re.$1, re.$2, re.$3 ], function(x){
return x.indexOf("%") > 0 ?parseFloat(x, 10) * 2.55 : x |
});
}
}
반환 함수(색상, 단계){
var ret = [], len = colors.length;
if ( 단계 === 정의되지 않음 ) { 단계 = 20 }
if (len == 1) {
ret = GetStep( 색상[0], 색상[0], 단계 );
} else if ( len > 1 ) {
for(var i = 0, n = len - 1; i < n; i++){
var steps = GetStep( 색상[i], 색상[i+1], 단계 );
i < n - 1 && steps.pop();
ret = ret.concat(단계);
}
}
반환 ret;
}
})();
//그라디언트 객체
var ColorTrans = function(elem, 옵션){
this._elem = $$(elem);
this._timer = null;//타이머
this._index = 0;//인덱스
this._colors = [];//색상 컬렉션
this._options = {};//매개변수 객체
this._setOptions(옵션);
this.speed = Math.abs(this.options.speed);
this.style = this.options.style;
this.reset({
출처: this.options.from || $$D.getStyle(this._elem, this.style),
받는 사람: this.options.to,
단계: Math.abs(this.options.step)
});
this._set();
};
ColorTrans.prototype = {
//기본 속성 설정
_setOptions: 함수(옵션) {
this.options = {//기본값
from: "",//시작 색상(자동 획득을 용이하게 하기 위한 기본 빈 값)
to: "#000",//색 끝
단계: 20,//그라디언트 시리즈
속도: 20,//그라디언트 속도
style: "color"//속성 설정(스크립팅 속성)
};
$$.extend(this.options, 옵션 || {});
},
//색상 컬렉션 재설정
재설정: 기능(옵션) {
//매개변수에 따라 속성 설정
this._options = 옵션 = $$.extend( this._options, 옵션 || {} );
//색상 컬렉션 가져오기
this._colors = ColorGrads( [ options.from, options.to ], options.step );
this._index = 0;
},
//색상 페이드 인
transIn: 함수() {
this.stop(); this._index++;
if(this._index < this._colors.length - 1){
this._timer = setTimeout($$F.bind( this.transIn, this ), this.speed);
}
},
// 색상이 희미해짐
transOut: 함수() {
this.stop(); this._index--;
if(this._index > 0){
this._timer = setTimeout($$F.bind( this.transOut, this ), this.speed);
}
},
//색상 설정
_set: 함수() {
var color = this._colors[Math.min(Math.max(0, this._index), this._colors.length - 1)];
this._elem.style[this.style] = "rgb(" + color.join(",") + ")";
},
//멈추다
중지: 함수() {
ClearTimeout(this._timer);
}
};
</script>
</head>
<본문>
<스타일 유형="텍스트/css">
#idGrads div{height:1px;overflow:hidden;}
</style>
색상 그라데이션 변경 데모:
<div id="idGrads"> </div>
<Br />
<input id="idGradsColor" type="text" value="green"/>
<input id="idGradsSet" type="button" value="색상 설정">
<스크립트>
($$("idGradsSet").onclick = 함수(){
var colors = ColorGrads( ["#fff", $$("idGradsColor").value], 50 );
$$("idGrads").innerHTML = $$A.map(colors.concat().concat(colors.reverse()), function(x){
"<div style='Background:rgb(" + x.join(",") + ")'></div>"를 반환합니다.
}).가입하다("");
})()
</script>
<Br />
<Br />
<스타일 유형="텍스트/css">
#idPicker ul{float:left;}
#idPicker li{overflow:hidden;width:10px;height:10px;line-height:0;font-size:0;}
#idPicker li.on{width:8px;height:8px; border:1px solid #000;}
#idPicker ul, #idPicker li{list-style:none;margin:0;padding:0;}
#idPicker:after{content:".";display:block;visibility:hidden;height:0;clear:both;}
#idPicker{zoom:1;}
</style>
단순 색상 선택기: <Br />
색상 선택: <span id="idPickerShow"> </span>
<div id="idPicker"> </div>
<스크립트>
$$("idPicker").innerHTML = "<ul>" +
$$A.map(ColorGrads( ["red", "yellow", "blue"], 10 ), function(x){
return $$A.map(ColorGrads( ["#fff", "rgb(" + x.join(",") + ")" ,"#000"], 10 ),
함수(x){
return "<li style='Background:rgb(" + x.join(",") + ")'></li>";
}
).가입하다("");
}).join("</ul><ul>") + "</ul>";
var oPickerShow = $$("idPickerShow");
$$A.forEach($$("idPicker").getElementsByTagName("li"), function(li){
li.onmouseover = 함수(){
this.className = "on";
oPickerShow.innerHTML = oPickerShow.style.BackgroundColor
= this.style.BackgroundColor;
}
li.onmouseout = function(){ this.className = "" };
})
</script>
<Br />
<Br />
<스타일 유형="텍스트/css">
#idMenu{ 배경:#DBDBDB;text-align:center;line-height:25px; 테이블 레이아웃:고정;}
#idMenu td{ 커서:포인터; 색상:#666;
</style>
색상 그라데이션 메뉴:
<table id="idMenu" width="600" border="0" cellpacing="5" cellpadding="0">
<tr>
<td> <a href=" http://www.cnblogs.com/cloudgamer/archive/2008/07/21/ImgCropper.html">크로퍼</a></td >
<td> <a href=" http://www.cnblogs.com/cloudgamer/archive/2009/01/06/Tween.html">트윈</a></td >
<td> <a href=" http://www.cnblogs.com/cloudgamer/archive/2008/12/24/Slider.html">슬라이더</a></td >
<td> <a href=" http://www.cnblogs.com/cloudgamer/archive/2008/12/03/Resize.html">크기 조정</a></td >
<td> <a href=" http://www.cnblogs.com/cloudgamer/archive/2008/11/17/Drag.html">드래그</a></td >
<td> <a href=" http://www.cnblogs.com/cloudgamer/archive/2009/07/07/FixedTips.html">도구 설명</a></td >
</tr>
</table>
<스크립트>
$$A.forEach($$("idMenu").getElementsByTagName("td"), function(x, i){
var a = x.getElementsByTagName("a")[0], href = a.href, txt = a.innerHTML;
x.onclick = function(){ location.href = href }
x.innerHTML = txt;
var ct1 = new ColorTrans(x, { to: "white" });
var ct2 = new ColorTrans(x, { 대상: "rgb(20,150,0)", 스타일: "배경색상" });
x.onmouseover = function(){ ct1.transIn() };
x.onmouseout = function(){ ct1.transOut() };
})
</script>
<Br />
<Br />
<스타일 유형="텍스트/css">
#idRandom{패딩:10px; 배경:#CCC;
</style>
임의의 색상 그라데이션을 클릭합니다.
<div id="idRandom"><b>색상을 무작위로 변경하려면 클릭하세요.</b></div>
<스크립트>
var ctRandom = new ColorTrans("idRandom", { style: "BackgroundColor" }),
ctRandom2 = new ColorTrans("idRandom");
$$("idRandom").onclick = 함수(){
var rgb = $$A.map([1,1,1], function(){ return Math.floor((Math.random() * 255)); } ),
rgb2 = $$A.map(rgb, function(x){ return 255 - x; } );
ctRandom.reset({ 원본: this.style.BackgroundColor, 대상: "rgb(" + rgb.join(",") + ")" })
ctRandom.transIn();
ctRandom2.reset({ 원본: this.style.color, 대상: "rgb(" + rgb2.join(",") + ")" })
ctRandom2.transIn();
}
</script>
</body>
</html>