Creating Scrolling Text With HTML, CSS, and JavaScript

When you’ve been building web applications for over 25 years, as I have done, using HTML, CSS, and JavaScript has become second nature. 

In this article, I’ll show you some simple ways to create scrolling text using these tools, including five different methods for coding scrolling text with plain HTML, HTML and CSS, and finally, HTML + CSS + JS.

1. Plain HTML Scrolling Text

Simply add a tag around the text to create a scrolling effect. This tag offers a few interesting parameters, among which:

  • direction — can be left, right, up, or down
  • scrollamount — the speed of the scroll text
  • loop — how many times should the scrolling repeat

NOTE: This is a deprecated HTML tag, so I’d recommend restraint when using it for modern web projects.

2. Animated Scrolling Text With CSS

Define a CSS animation called scroll that animates the translateX property.

CSS

 

Then apply that animation to your text element:
HTML

 

By adding this CSS:
Plain Text

 

Changing from translateX to translateY will give you a vertical scrolling text. Switching 0% and 100% around will give you the reverse scrolling text direction. And in the .scrolling-text CSS class, if you change the 10s duration of the animation, you change the speed of the scrolling text.

3. HTML + CSS + JavaScript Scrolling Text

HTML code:

HTML

 

CSS code:

CSS

 

JavaScript code:
JavaScript

 

This is my most “unelegant” solution for making scrolling text. Basically, it’s cramming text in a container with a scroll and changing the scrolling of the container programmatically. When the scroll amount goes over the container amount, it resets.

4. Scroll Text With jQuery

CSS

 

Use the animate() jQuery function to animate the scrollLeft property, and this will create a scrolling text effect.

In my view, jQuery is a bit overkill in this situation, and it only makes sense if you have already used jQuery in your project.

Of course, animate() can also be used for animating the translateX or translateY properties as seen above.

5. Scrolling Text With Canvas HTML5

This is my favorite method. Especially because it’s so flexible and offers so many possibilities, like, for example, exporting the scrolling text as GIF or even video. You can see this in action by going to the Scrolling Text generator on PSDDude, where you can create your own customized scrolling text images and videos.

The HTML code is straightforward:

HTML

 

And the JS is where the magic happens:
JavaScript

 

Using a loop with requestAnimationFrame() calling the function draw() is actually the way HTML5 games implement their graphics drawing. This is a cool way to create smooth-scrolling text.

You get the size on screen of the text using the measureText() context method. This allows creating a seamless scrolling text by resetting the text position when it reaches the end position.

Bonus: A Few Scrolling Text Ideas

LED Scrolling Text GIF

Learn more about it in this link.

LED Scrolling Text

Star Wars Opening Crawl Scrolling Text Generator

Learn more about it in this link.

Stock Market Scrolling Text

Learn more here.
Stock Market Scrolling Text

Weather Scrolling Text

Learn more about it here.

These were created with the scrolling text gif and video generator on PSDDude.

Conclusion

Now you know how to make scrolling text using HTML, CSS, and/or JavaScript.

What will you use this for? Drop me a comment to let me know. Also, if you feel I’ve missed an important method for creating scrolling text, give me your thoughts on that.

Source:
https://dzone.com/articles/scrolling-text-html-css-javascript