Introduction

This article explains how to use a marquee with HTML and JavaScript. A marquee is used to move text from right to left, left to right, up and down and down and up.

HTML Marquee

  1. <marquee behavior="alternate" scrolldelay="100"><marquee width="100">
  2. <h5 style="color:red; font-weight: bold; font-size: large;">Csharpcorner</h5>
  3. </marquee></marquee>

Marquee Type

JavaScript Marquee Funcation
  1. function scroll(pos)
  2. {
  3. var txt = "C-sharpcorner";
  4. var output = "";
  5. var screen = document.getElementById("wordscroller");
  6. for (var i = 0; i < pos; i++)
  7. {
  8. output += txt.charAt(i);
  9. }
  10. output += txt.charAt(pos);
  11. screen.innerHTML = output;
  12. pos++;
  13. if (pos != txt.length)
  14. {
  15. window.setTimeout(function () { scroll(pos); }, 200);
  16. }
  17. else
  18. {
  19. window.setTimeout(function () { scroll(0); }, 5000);
  20. }
  21. }
Complete Program
Marquee.html
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title></title>
  5. </head>
  6. <body>
  7. <h3>HTMl Marquee</h3>
  8. <marquee behavior="alternate" scrolldelay="100"><marquee width="100">
  9. <h5 style="color:red; font-weight: bold; font-size: large;">Csharpcorner</h5>
  10. </marquee></marquee>
  11. <h3>JavaScript Marquee</h3>
  12. <p id="wordscroller" style="color: #0000FF; font-weight: normal; font-size: x-large"></p>
  13. <script type="text/javascript">
  14. function scroll(pos)
  15. {
  16. var txt = "C-sharpcorner";
  17. var output = "";
  18. var screen = document.getElementById("wordscroller");
  19. for (var i = 0; i < pos; i++)
  20. {
  21. output += txt.charAt(i);
  22. }
  23. output += txt.charAt(pos);
  24. screen.innerHTML = output;
  25. pos++;
  26. if (pos != txt.length)
  27. {
  28. window.setTimeout(function () { scroll(pos); }, 200);
  29. }
  30. else
  31. {
  32. window.setTimeout(function () { scroll(0); }, 5000);
  33. }
  34. }
  35. scroll(0);
  36. </script>
  37. </body>
  38. </html>
Output
Ani.gif
For more information, download the attached sample application.