javascript 根据3原色制作颜色选择器

先上一张效果图:

demo:http://www.yxsss.com/ui/yanse1.html
易U :http://www.yxsss.com/ui/

javascript 代码:

  1 document.write('<div id="msecolor"><div class="aa"><div class="ab"><div class="ab1"><select onchange="T.f(this.value)"><option value="1">红</option><option value="3">绿</option><option value="5">蓝</option><option value="7">灰</option></select></div><div id="msecolor_ab2"><input type="text" maxlength="7"><span></span></div></div><div class="ac"><div id="msecolor_ac1"></div><div id="msecolor_ac2"></div></div></div></div>');
  2 T={s:true};
  3 T.a=A.$('msecolor_ab2').children[0];
  4 T.b=A.$('msecolor_ab2').children[1];
  5 T.c=A.$('msecolor_ac1');
  6 T.d=A.$('msecolor_ac2');
  7 T.ini=function(id,fun){
  8     this.fun=fun;
  9     if(typeof(id)!='object')
 10     id=A.$(id);
 11     this.obj=id;
 12     var w=A.wz(id);
 13     var ph=document.documentElement.clientHeight,pw=document.documentElement.clientWidth;
 14     var sh=document.documentElement.scrollTop,sw=document.documentElement.scrollLeft;
 15     if(w.x-sw+322 > pw){
 16         var l=w.x+id.offsetWidth - 322;
 17     }else{
 18         var l=w.x;
 19     }
 20     if(w.y-sh+329+id.offsetHeight > ph){
 21         var t=w.y - 329;
 22     }else{
 23         var t=w.y+id.offsetHeight;
 24     }
 25     with(A.$('msecolor').style){
 26         display='block';
 27         top=t+'px';
 28         left=l+'px';
 29     }
 30 }
 31 T.hide=function(){A.$('msecolor').style.display='none';}
 32 T.e=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
 33 T.f=function(a){
 34     T.h=a;
 35     T.c.innerHTML='';
 36     if(a==7){
 37         for(var i=0;i<16;i++){
 38         var s=A.$$('span');
 39         s.title=s.style.backgroundColor='#'+T.e[i]+T.e[i]+T.e[i]+T.e[i]+T.e[i]+T.e[i];
 40         T.c.appendChild(s);
 41         }
 42         T.g(a,0);
 43     }else{
 44     for(var i=0;i<16;i++){
 45     var s=A.$$('span');
 46     s.title=s.style.backgroundColor=T.z(a,T.e[i]);
 47     T.c.appendChild(s);
 48     s.onclick=function(){
 49         var s=this.title.substr(1,1);
 50         if(s=='0')
 51         s=this.title.substr(3,1);
 52         if(s=='0')
 53         s=this.title.substr(5,1);
 54         T.g(T.h,s);
 55         }
 56     }
 57     T.g(a,0);
 58     }
 59 }
 60 T.g=function(a,b){
 61     T.d.innerHTML='';
 62     switch(parseInt(a)){
 63         case 1:
 64         var c=3,e=5,g='T.z(a,b,c,T.e[parseInt(i/16)],e,T.e[i%16])';
 65         break;
 66         case 3:
 67         var c=1,e=5,g='T.z(c,T.e[parseInt(i/16)],a,b,e,T.e[i%16])';
 68         break;
 69         case 5:
 70         var c=1,e=3,g='T.z(c,T.e[parseInt(i/16)],e,T.e[i%16],a,b)';
 71         break;
 72         case 7:
 73         for(var i=0;i<256;i++){
 74             var s=document.createElement('span');
 75             s.title=s.style.backgroundColor='#'+T.e[parseInt(i/16)]+T.e[i%16]+T.e[parseInt(i/16)]+T.e[i%16]+T.e[parseInt(i/16)]+T.e[i%16];
 76             s.onmouseover=function(){
 77                 T.a.value=this.title;
 78                 T.b.style.backgroundColor=this.style.backgroundColor;
 79                 T.b.title=this.title;
 80             }
 81             s.onclick=function(){
 82                 T.hide();
 83                 T.fun(this.title,T.obj);
 84             }
 85             T.d.appendChild(s);
 86         }
 87         return false;
 88         break;
 89     }
 90     for(var i=0;i<256;i++){
 91     var s=document.createElement('span');
 92     s.title=s.style.backgroundColor=eval(g);
 93     s.onmouseover=function(){
 94         T.b.style.backgroundColor=this.style.backgroundColor;
 95         T.a.value=this.title;
 96         T.b.title=this.title;
 97     }
 98     s.onclick=function(){
 99         T.hide();
100         T.fun(this.title,T.obj);
101     }
102     T.d.appendChild(s);
103     }
104 }
105 T.b.onclick=function(){
106     T.hide();
107     T.fun(this.title,T.obj);
108 }
109 T.a.onkeyup=function(e){
110     var e=e || event;
111     if(e.keyCode==13){
112         T.b.style.backgroundColor=this.value;
113         T.b.title=this.value;
114         T.hide();
115         T.fun(this.value,T.obj);
116     }
117 }
118 T.z=function(a,b,c,d,e,f){s='#';a=parseInt(a);c=parseInt(c);e=parseInt(e);for(var i=0;i<6;i++){if(i>=(a-1) && i<(a+1)){s+=b;}else if(i>=(c-1) && i<(c+1)){s+=d;}else if(i>=(e-1) && i<(e+1)){s+=f;}else{s+='0';}}return s;}
119 T.f(1);
原文地址:https://www.cnblogs.com/jinging/p/2867866.html