• We just launched and are currently in beta. Join us as we build and grow the community.

Color Effect in Text using JavaScript

lowmatild

AI Systems Architect
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
142
Likes
148
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this article, we are going to learn about Color Effect in Text using JavaScript. You can use this to your future projects or websites to attract the user. This is very simple kind of source code, you can easily change the base color, text color, and the flash speed in the text.

In our example, we have white color for the base, yellow color for the text, and 80 milliseconds for the flash speed.

This is the script source code that you can edit the color and the speed.

  1. <

    script language=

    "JavaScript1.2"

    >
  2. var

    message =

    "Welcome to Sourcecodester! Free source code."
  3. var

    neonbasecolor =

    "white"
  4. var

    neontextcolor =

    "yellow"
  5. var

    flashspeed =

    80

    //in milliseconds
  6. </

    script>

Complete Script Source Code

Kindly copy and paste this simple script source code to your BODY tag of your page. To have a great neon effect in your text.

  1. <

    script language=

    "JavaScript1.2"

    >
  2. var

    message =

    "Welcome to Sourcecodester! Free source code."
  3. var

    neonbasecolor =

    "white"
  4. var

    neontextcolor =

    "yellow"
  5. var

    flashspeed =

    80

    //in milliseconds

  6. ///No need to edit below this line/////
  7. var

    n=

    0
  8. if

    (

    document.all

    ||

    document.getElementById

    )

    {
  9. document.write

    (

    ''

    )
  10. for

    (

    m=

    0

    ;

    m<

    message.length

    ;

    m++

    )
  11. document.write

    (

    '<span id="neonlight'

    +

    m+

    '">'

    +

    message.charAt

    (

    m)

    +

    '</span>'

    )
  12. document.write

    (

    ''

    )
  13. }
  14. else
  15. document.write

    (

    message)

  16. function

    crossref(

    number)

    {
  17. var

    crossobj=

    document.all

    ?

    eval(

    "document.all.neonlight"

    +

    number)

    :

    document.getElementById

    (

    "neonlight"

    +

    number)
  18. return

    crossobj
  19. }

  20. function

    neon(

    )

    {
  21. //Change all letters to base color
  22. if

    (

    n==

    0

    )

    {
  23. for

    (

    m=

    0

    ;

    m<

    message.length

    ;

    m++

    )
  24. //eval("document.all.neonlight"+m).style.color=neonbasecolor
  25. crossref(

    m)

    .style

    .color

    =

    neonbasecolor
  26. }
  27. //cycle through and change individual letters to neon color
  28. crossref(

    n)

    .style

    .color

    =

    neontextcolor
  29. if

    (

    n<

    message.length

    -

    1

    )
  30. n++
  31. else

    {
  32. n=

    0
  33. clearInterval(

    flashing)
  34. setTimeout(

    "beginneon()"

    ,

    1500

    )
  35. return
  36. }
  37. }

  38. function

    beginneon(

    )

    {
  39. if

    (

    document.all

    ||

    document.getElementById

    )
  40. flashing=

    setInterval(

    "neon()"

    ,

    flashspeed)
  41. }
  42. beginneon(

    )
  43. </

    script>

Output

This is the output after constructing the script in your editor.

untitled_12.jpg


Kindly click the "Download Code" button below for full source code. Thank very much. Enjoy coding.

Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top