Introduction
In this article we will create a text effect using JavaScript. This effect shows a character input one by one instead of an entire block at a time. The timing of the effect can also be customized using a slider. So let's start our explanation.
LIVE DEMO
Text effect V1
To implement this effect we need the following things.
- First of all we need one div, let's say "text", element that will contain our entire effect.
- We are also must have one paragraph of text on which we will apply our effect. This paragraph is to be placed inside the "text" div.
- To control the effect we are also required to have one timer. That timer will control the speed of our text effect.
- Apart from the above necessary things I'm also placing a slider below the "text" div so that the user can adjust the speed of animation using it.
HTML for slider
CSS for slider
JavaScript for Slider
The code above works in the following ways.
- Line number 2 extracts all the text of the paragraph on which the effect is to be applied.
- Line number 3 clears the text of the paragraph after extracting the text in line number 2.
- Line number 6 initiates a timer with a 10 millisecond interval. After each interval the timer will call the "addChar" function. This timer can be identified using its interval id that is stored in iId.
- Line numbers 7 to 15 is the body of the "addChar" function. This function simply adds one character to a paragraph each time it is called. When all the characters are added to the paragraph the timer is canceled. This cancellation is performed in line number 13.
- Line numbers 16-18 defines a jQuery slider with some options.
- Line numbers 19-25 are the part of on slide event handler. This event handler is executed when the slider is changed.
- The slider handler clears all the text, resets the timer and initiates an animation with new timings.
Output
![Output]()
![Output]()
![Output]()
Summary
That's all for this article. I hope you have enjoyed reading it and found it useful. In case of any doubt feel free to ask in the comments.
LIVE DEMO