Text Effect V2 Using jQuery and JavaScript

Introduction

In this article we will create a text effect using jQuery effects. This effect shows a stream of characters flowing one by one instead of an entire block at a time with the scaled upcoming characters as animation. The timing of the effect can also be customized using a slider. So let's start our explanation.

LIVE DEMO

Text effect V2

To implement this effect we need the following things:

  • First of all we need one div, let's say a "text" element that will contain our block of text.
     
  • We are also required to have one paragraph of text on which we will apply our effect. This paragraph is to be placed inside the "text" div.
     
  • We also need one class that will contain the settings for animated text. We will apply this class on each character and remove it once the animation gets over.
     
  • To control the effect we are also required to have one timer. That timer will control the speed of our effect.
     
  • Apart from above necessary things we are also required a slider below the "text" div so that user can adjust the speed of animation using it.

HTML for Text Effect V2

<!DOCTYPE html>

<html>

<head>

<meta name="description" content="Text effect" />

<link href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>

<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>

  <meta charset="utf-8">

  <title>JS Bin</title>

</head>

<body>

  <div id="text">

    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

  </div>

  <div id="slider"></div>

  <div id="val">100</div>

</body>

</html>

CSS for slider

#text{

  width:400px;

  height:auto;

  position:fixed

  border:1px solid;

}

 

#slider{

  display:inline-block;

  margin:10px 0 0 10px;

  width:300px;

}

#val{

  display:inline-block;

  width:auto;

  height:20px;

  border:1px solid;

}

 

.eft-b{

 color:red;

 font-size:50px;

}

  

JavaScript for Slider

  1. $(document).ready(function(){

  2.   var text=$("#text > p").text();

  3.   $("#text > p").text("");

  4.   var i=0;

  5.   var iId;

  6.   var time=100;

  7.   iId=setInterval(addChar,time);

  8.   function addChar(){

  9.    var old=$("p").text();

  10.    var n=old+text[i];

  11.     $("#text > p").append("<span class='eft-b' id='ef"+i+"'>"+text[i]+"</span>");

  12.     $("#ef"+i).hide().show();

  13. $("#ef"+i).removeClass("eft-b",1000);

  14.     i++;

  15.    if(i>= text.length){

  16.       clearInterval(iId);

  17.     $("#text > p").text(""); 

  18.   $("#text > p").text(text);

  19.      

  20.     }

  21.   }

  22.   $("#slider").slider({"min":10,

  23.                        "max":1000,

  24.                        "step":100,

  25.     slide:function(){

  26.       clearInterval(iId);

  27.   $("#text > p").text("");

  28.       i=0;

  29.       time=$(this).slider("value");

  30.       iId=setInterval(addChar,time);

  31.       $("#val")

  32. .text($(this).slider("value"));

  33.     }

  34.   });

  35. });

The code above works in the following steps:

  1. Line number 2 extracts all the text of the paragraph on which the effect is to be applied.
     
  2. Line number 3 clears the text of the paragraph after extracting the text in line number 2.
     
  3. Line number 7 initiates a timer with a 100 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.
     
  4. Line numbers 8 to 21 is the "addChar" function. This function simply adds one character to a paragraph each time it is called. When all the characters are added to a paragraph the timer is canceled. This cancellation is performed in line number 16. This function is a bit more complex than it first appears to be. In line number 11 we are adding a span tag with "eft-b" class containing only one character of text. In line number 12 we are hiding this element and then showing it with the animation. Line number 13 is the main line that is causing the text animation.
     
  5. In line numbers 17 and 18 we are removing all the spans we added earlier for animation and they are replaced with the entire text. This keeps our DOM short.
     
  6. Line numberw 22-34 defines a jQuery slider with some options.
     
  7. Line numbers 25-33 are part of the slide event handler. This event handler is executed when the slider is changed.
     
  8. The slider handler clears all the text, resets the timer and initiates an animation with new timings.

 

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 comments.

 

LIVE DEMO

Up Next
    Ebook Download
    View all
    Learn
    View all