专题栏目
  • 濠电姷鏁搁崑鐐哄垂鐠轰警娼栫紓浣股戦崣蹇涙煕閹炬瀚惔濠囨煙閸忚偐鏆橀柛鏂块閻e灚绗熼埀顒勫箖濡ゅ懎鎹舵い鎾跺剱閳ь剙鐭傞弻娑㈡倷閼碱剛楔闂佽鍠栫紞濠傜暦閸洘鐓ラ悗锝庝憾娴硷拷
  • 闂傚倸鍊风粈渚€骞夐敓鐘参﹂柣鎰暩閻棗銆掑锝呬壕閻庤娲╃紞渚€鐛鈧鍫曞箣閻樼數宓侀梺鑽ゅ枑缁瞼绮旈崜浣虹=闁规崘顕х粻姘辨喐濠婂牆鍚规俊銈呮噺閻撱儵鏌i弴鐐测偓鍦偓姘炬嫹
  • 缂傚倸鍊搁崐椋庣矆娓氣偓钘濆ù鍏兼綑閸ㄥ倿鏌i姀鐘差棌闁轰礁妫濋弻锝夊籍閸屾艾浠橀梺娲诲幗椤ㄥ﹪寮婚悢琛″亾濞戞瑡缂氱紒鐘成戠换娑㈠礂閸忕⒈妫冮梺璇″枛閸㈡煡鍩㈡惔銈囩杸闁圭虎鍨版禍楣冩煕濠靛嫬鍔ょ紒鎲嬬畵閺岀喖顢涢崱妤勫婵炲牜鍋婂铏规崉閵娿儲鐝㈤梺鐟板殩閹凤拷
  • 闂傚倷娴囧畷鐢稿窗閹扮増鍋¢弶鍫氭櫅缁躲倕螖閿濆懎鏆欐俊顐C妴鎺戭潩閿濆懍澹曟俊銈囧Х閸嬫劙宕戦幘鏂ユ斀闁绘ḿ绮☉褎绻涚仦鍌氣偓婵嗙暦閵忋倕绠虫俊銈呭暙瑜板嫰姊洪幖鐐插妧闁告劏鏅滈鍕磽閸屾瑦绁版い鏃€鍔欓獮蹇涙晸閿燂拷
  • 闂傚倸鍊风粈渚€骞夐敓鐘参﹂柣鎰▕濞撳鏌熼悜妯烩拻闁告宀稿娲垂椤曞懎鍓扮紓浣稿閸嬬喖骞夌粙娆惧悑闁搞儮鏅濇径鍕⒑鐠団€崇€婚悘鐐跺Г椤斿倹绻濈喊澶岀?闁稿绋掗弲鍫曟偩瀹€鈧惌鎾绘煟閹惧磭鐦嶉柛锔诲幗閸忔粓姊洪崹顕呭剰妞ゅ骏鎷�
  • 您现在的位置: 军旅同心 >> 读书赏析 >> 学习园地 >> 电脑网络 >> 技术文章 >> 文章正文
    用javascript实现变色背景和文字
    作者:采集员 文章来源:来源于网络 点击数:412 更新时间:2005-9-10 14:26:07
    用JavaScript实现变色背景和文字

      制作变色背景和文字的JavaScript代码有很多种,不过大多数背景颜色变化时有跳跃感,不够柔和。本例的效果特别酷!

    请看效果:



    背景请用bgcolor参数,前景文字请用fgcolor参数。

    全部代码如下:

    1、背景:

    < html >
    < head >
    < title >bgcolor< /title >
    < meta http-equiv="Content-Type" content="text/html; charset=gb2312" >
    < script language="JavaScript" >
    < !--
    //you can assign the initial color of the background here
    r=255;
    g=50;
    b=50;
    flag=0;
    t=new Array;
    o=new Array;
    d=new Array;

    function hex(a,c)
    {
    t[a]=Math.floor(c/16)
    o[a]=c%16
    switch (t[a])
    {
    case 10:
    t[a]='A';
    break;
    case 11:
    t[a]='B';
    break;
    case 12:
    t[a]='C';
    break;
    case 13:
    t[a]='D';
    break;
    case 14:
    t[a]='E';
    break;
    case 15:
    t[a]='F';
    break;
    default:
    break;
    }
    switch (o[a])
    {
    case 10:
    o[a]='A';
    break;
    case 11:
    o[a]='B';
    break;
    case 12:
    o[a]='C';
    break;
    case 13:
    o[a]='D';
    break;
    case 14:
    o[a]='E';
    break;
    case 15:
    o[a]='F';
    break;
    default:
    break;
    }
    }

    function ran(a,c)
    {
    if ((Math.random() >2/3||c==0)&&c< 255)
    {
    c++
    d[a]=2;
    }
    else
    {
    if ((Math.random()< =1/2||c==255)&&c >0)
    {
    c--
    d[a]=1;
    }
    else d[a]=0;
    }
    return c
    }
    function do_it(a,c)
    {
    if ((d[a]==2&&c< 255)||c==0)
    {
    c++
    d[a]=2
    }
    else
    if ((d[a]==1&&c >0)||c==255)
    {
    c--;
    d[a]=1;
    }
    if (a==3)
    {
    if (d[1]==0&&d[2]==0&&d[3]==0)
    flag=1
    }
    return c
    }
    function disco()
    {
    if (flag==0)
    {
    r=ran(1, r);
    g=ran(2, g);
    b=ran(3, b);
    hex(1,r)
    hex(2,g)
    hex(3,b)
    document.bgColor="#"+t[1]+o[1]+t[2]+o[2]+t[3]+o[3]
    flag=50
    }
    else
    {
    r=do_it(1, r)
    g=do_it(2,g)
    b=do_it(3,b)
    hex(1,r)
    hex(2,g)
    hex(3,b)
    document.bgColor="#"+t[1]+o[1]+t[2]+o[2]+t[3]+o[3]
    flag--
    }
    if (document.all)
    setTimeout('disco()',20)
    }
    //-- >
    < /script >
    < /head >

    < body onload="disco()" bgcolor="#FFFFFF" text="#000000" >
    < /body >
    < /html >
    2、前景文字:


    < html >
    < head >
    < title >bgcolor< /title >
    < meta http-equiv="Content-Type" content="text/html; charset=gb2312" >
    < script language="JavaScript" >
    < !--
    //you can assign the initial color of the background here
    r=255;
    g=0;
    b=0;
    flag=0;
    t=new Array;
    o=new Array;
    d=new Array;
    function hex(a,c)
    {
    t[a]=Math.floor(c/16)
    o[a]=c%16
    switch (t[a])
    {
    case 10:
    t[a]='A';
    break;
    case 11:
    t[a]='B';
    break;
    case 12:
    t[a]='C';
    break;
    case 13:
    t[a]='D';
    break;
    case 14:
    t[a]='E';
    break;
    case 15:
    t[a]='F';
    break;
    default:
    break;
    }
    switch (o[a])
    {
    case 10:
    o[a]='A';
    break;
    case 11:
    o[a]='B';
    break;
    case 12:
    o[a]='C';
    break;
    case 13:
    o[a]='D';
    break;
    case 14:
    o[a]='E';
    break;
    case 15:
    o[a]='F';
    break;
    default:
    break;
    }
    }

    function ran(a,c)
    {
    if ((Math.random() >2/3||c==0)&&c< 255)
    {
    c++
    d[a]=2;
    }
    else
    {
    if ((Math.random()< =1/2||c==255)&&c >0)
    {
    c--
    d[a]=1;
    }
    else d[a]=0;
    }
    return c
    }
    function do_it(a,c)
    {
    if ((d[a]==2&&c< 255)||c==0)
    {
    c++
    d[a]=2
    }
    else
    if ((d[a]==1&&c >0)||c==255)
    {
    c--;
    d[a]=1;
    }
    if (a==3)
    {
    if (d[1]==0&&d[2]==0&&d[3]==0)
    flag=1
    }
    return c
    }
    function disco()
    {
    if (flag==0)
    {
    r=ran(1, r);
    g=ran(2, g);
    b=ran(3, b);
    hex(1,r)
    hex(2,g)
    hex(3,b)
    document.fgColor="#"+t[1]+o[1]+t[2]+o[2]+t[3]+o[3]
    flag=50
    }
    else
    {
    r=do_it(1, r)
    g=do_it(2,g)
    b=do_it(3,b)
    hex(1,r)
    hex(2,g)
    hex(3,b)
    document.fgColor="#"+t[1]+o[1]+t[2]+o[2]+t[3]+o[3]
    flag--
    }
    if (document.all)
    setTimeout('disco()',0)
    }
    //-- >
    < /script >
    < /head >

    < body onload="disco()" text="#000000" style="font:25pt 黑体" >赛迪网电脑应用
    < /body >
    < /html >


     
    免责声明:作品版权归所属媒体与作者所有!!本站刊载此文不代表同意其说法或描述,仅为提供更多信息。如果您认为我们侵犯了您的版权,请告知!本站立即删除。有异议请联系我们。
    文章录入:烟灰缸    责任编辑:烟灰缸 
    高级搜索
       
    网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
        没有任何评论
    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告 | 管理登录 |