JavaScript颜色表

Fireworks中选择颜色的时候,如图,当鼠标单击图中红线圈住的地方就会弹出一个颜色表,在颜色表中选取一种颜色后,图中红线圈住的地方就会变成选取的颜色,同时颜色表消失。
image

下面让我们用JavaScript在网页中也加入类似功能,实际效果和代码如下:
image
<script>
function findObj(n, d){
    var p,i,x;
    if(!d)d=document;
    if((p=n.indexOf("?"))>0 && parent.frames.length){
        d=parent.frames[n.substring(p+1)].document;
        n=n.substring(0,p);
    }
    if(!(x=d[n]) && d.all)x=d.all[n];
    for(i=0; !x && i<d.forms.length; i++)x=d.forms[i][n];
    for(i=0; !x && d.layers && i<d.layers.length; i++)x=findObj(n,d.layers[i].document);
    if(!x && d.getElementById)x=d.getElementById(n);
    return x;
}
function colorsa(c){
    findObj('colordiv').style.display='none';
    findObj('colortd').style.backgroundColor='#'+c;
    alert('#'+c); //修改此处可达到其他不同的效果
}
var hexch=new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
function ToHex(n){
    var h, l;
    n=Math.round(n);
    l=n%16;
    h=Math.floor((n / 16)) % 16;
    return (hexch[h]+hexch[l]);
}
function ToHex1(n){
    var l1;
    n=Math.round(n);
    l1=n % 16;
    return (hexch[l1]+hexch[l1]);
}
function wc(r, g, b, n){
    r=((r*16+r)*3*(15 - n)+0x80*n) / 15;
    g=((g*16+g)*3*(15 - n)+0x80*n) / 15;
    b=((b*16+b)*3*(15 - n)+0x80*n) / 15;
    document.write('<td width="8" bgcolor="#'+ToHex(r)+ToHex(g)+ToHex(b)+'" οnclick="colorsa(/''+ToHex(r)+ToHex(g)+ToHex(b)+'/');" οnmοuseοver="this.style.backgroundImage=/'url(color_bg.gif)/';" οnmοuseοut="this.style.backgroundImage=/'/';"></td>');
}
var cnum=new Array(1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0);
function colortabl(){
    for(i=0; i < 15; i ++){
        document.write('<table border="0" cellpadding="0" cellspacing="0"><tr>');
        document.write('<td bgcolor="#'+ToHex1(i)+ToHex1(i)+ToHex1(i)+'" height="8" width="8" οnclick="colorsa(/''+ToHex1(i)+ToHex1(i)+ToHex1(i)+'/');" οnmοuseοver="this.style.backgroundImage=/'url(color_bg.gif)/';" οnmοuseοut="this.style.backgroundImage=/'/';"></td>')
        for(j=0; j < 30; j ++){
            n1=j % 5;
            n2=Math.floor(j / 5)*3;
            n3=n2+3;
            wc((cnum[n3]*n1+cnum[n2]*(5 - n1)),(cnum[n3+1]*n1+cnum[n2+1]*(5 - n1)),(cnum[n3+2]*n1+cnum[n2+2]*(5 - n1)), i);
        }
        document.writeln('</tr></table>');
    }
}
</script>
<div id="colordiv" style="display:none;position:absolute;z-index:5;" οnmοuseοut="this.style.display='none';" οnmοuseοver="this.style.display='';">
    <script type="text/JavaScript">colortabl();</script>
</div>
<table border="0" cellpadding="0" cellspacing="0" style="background-color:#000000;" id="colortd" οnclick="findObj('colordiv').style.display='';">
    <tr>
        <td><img src="color_ch.gif"></td>
    </tr>
</table>
</body>
</html>

需要的图片:
image color_bg.gif
image color_ch.gif

代码中注释的部分为单击颜色表中的颜色块时执行的动作,这里是弹出警告窗口,内容为选取的颜色代码,可以修改为其他效果。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值