Results 1 to 5 of 5

Thread: Fading text

  1. #1
    Join Date
    Nov 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Fading text

    Hello. I'm trying to make some tabbed content text fade in when the tab is selected. I have this script working:

    HTML Code:
    <script language="JavaScript">
    
    hex=255// Initial color value.
    
    function fadetext()
    
    {
    
    if(hex>0)
    
    {//If color is not black yet
    
    hex-=11;// increase color darkness
    
    document.getElementById("sample").style.color="rgb("+hex+","+hex+","+hex+")";
    
    setTimeout("fadetext()",100);
    
    }
    
    else
    
    hex=255//reset hex value
    
    }
    
    </script>
    

    The only thing is, I can't make the ending color anything but black. I've tried putting a hex code in place of the rgb("+hex+","+hex+","+hex+"), and changing the value in the hex < 0 command. Any ideas why it's not working? The text will show up as the color I want it to be, but there is no fading.

    Thanks for any help you can give!

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You could change it to:

    Code:
    if(hex>68)
    Then you would end up with some shade of gray, not black. But to get it to be a color, you would need to initialize 3 'hex' values and decrement each one individually toward the desired color.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Nov 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks. I tried changing

    style.color=rgb("+hex+","+hex+","+hex+")

    to

    style.color=rgb(102,102,139)

    and it gives me the color I want, but it disables the fade in. I also tried

    style.color="#666699"

    with the same result. I get the color, but no fade in.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Try this out:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Text Fader - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    
    /* Text Fader Script ©2008 John Davenport Scheuer
     * This notice must remain for legal use
     * As first seen in http://www.dynamicdrive.com/forums/ */
    
    function fadetext(id, init, targ, speed){
    var f=fadetext;
    if(f[id]&&f[id].t)
    clearTimeout(f[id].t)
    if(init){
    f[id]={};
    f[id].r=init[0],f[id].g=init[1],f[id].b=init[2];
    var t=255; f[id].m=[]
    f[id].m[0]=f[id].r<targ[0]? -1 : f[id].r>targ[0]? 1 : 0;
    f[id].m[1]=f[id].g<targ[1]? -1 : f[id].g>targ[1]? 1 : 0;
    f[id].m[2]=f[id].b<targ[2]? -1 : f[id].g>targ[2]? 1 : 0;
    for (var i = 0; i < init.length; i++)
    if(init[i]-targ[i])
    t=Math.min(t,Math.abs(init[i]-targ[i]));
    f[id].rt=Math[f[id].m[0]>0?'floor':'ceil']((f[id].r-targ[0])/t);
    f[id].gt=Math[f[id].m[1]>0?'floor':'ceil']((f[id].g-targ[1])/t);
    f[id].bt=Math[f[id].m[2]>0?'floor':'ceil']((f[id].b-targ[2])/t);
    }
    
    if(f[id].r==targ[0]&&f[id].g==targ[1]&&f[id].b==targ[2])//If color is back
    return; //exit
    
    if(f[id].m[0]&&f[id].r>targ[0]*f[id].m[0])
    f[id].r=Math[f[id].m[0]>0?'max':'min'](f[id].r-f[id].rt,targ[0]);// increase/decrease red darkness
    if(f[id].m[1]&&f[id].g>targ[1]*f[id].m[1])
    f[id].g=Math[f[id].m[1]>0?'max':'min'](f[id].g-f[id].gt,targ[1]);// increase/decrease green darkness
    if(f[id].m[2]&&f[id].b>targ[2]*f[id].m[2])
    f[id].b=Math[f[id].m[2]>0?'max':'min'](f[id].b-f[id].bt,targ[2]);// increase/decrease blue darkness
    
    document.getElementById(id).style.color="rgb("+f[id].r+","+f[id].g+","+f[id].b+")";
    
    f[id].t=setTimeout(function(){fadetext(id, false, targ, speed);},speed);
    
    }
    
    </script>
    </head>
    <body>
    <div id="sample" style="color:rgb(0, 128, 0);">
    Hey there!
    </div>
    <input type="button" onclick="fadetext('sample', [255,255,255], [0,128,0], 20);" value="fade">
    <div id="sample2" style="background-color:rgb(0, 128, 0);color:white;">
    Hey there!
    </div>
    <input type="button" onclick="fadetext('sample2', [0,128,0], [255,255,255], 20);" value="fade">
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Nov 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Awesome, that works! Thanks so much!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •