Skip to main content

Typing Text Effect In Javascript

Most of the time we need to create a effect just like typing text in web page but don't know how to do such task through javascript.
 
This article is for creating typing effect for given text through javascript, below is auto started, manual through started through button click demo is given below.

Demo Text :
Typing Text Effect In Javascript Hey! This is a demo article only for showing how typing text works, through javascript.

You can simply use this code by adding this javascript code and place your html as the given place or you can add more block like this just to need create more instance of TypingText Class.




<script type="text/javascript">

/****Class Defination****/
function TypingText (element, interval, cursor, finishedCallback) {
    if ((typeof document.getElementById == "undefined") || (typeof element.innerHTML == "undefined")) {
        this.running = true;
        return;
    }
    this.element = element;
    this.finishedCallback = (finishedCallback ? finishedCallback : function () { return; });
    this.interval = (typeof interval == "undefined" ? 100 : interval);
    this.origText = this.element.innerHTML;
    this.unparsedOrigText = this.origText;
    this.cursor = (cursor ? cursor : "");
    this.currentText = "";
    this.currentChar = 0;
    this.element.typingText = this;
    if (this.element.id == "") this.element.id = "typingtext" + TypingText.currentIndex++;
    TypingText.all.push(this);
    this.running = false;
    this.inTag = false;
    this.tagBuffer = "";
    this.inHTMLEntity = false;
    this.HTMLEntityBuffer = "";
}


/**** Static Valiables ****/
TypingText.all = new Array();
TypingText.currentIndex = 0;


/*** Static Fuction Add ***/
TypingText.runAll = function () {
    for (var i = 0; i < TypingText.all.length; i++) TypingText.all[i].run();
}

/*** Instance Fuction Add ***/
TypingText.prototype.run = function () {
    if (this.running) return;
    if (typeof this.origText == "undefined") {
        setTimeout("document.getElementById('" + this.element.id + "').typingText.run()", this.interval);
        return;
    }
    if (this.currentText == "") this.element.innerHTML = "";
    if (this.currentChar < this.origText.length) {
        if (this.origText.charAt(this.currentChar) == "<" && !this.inTag) {
            this.tagBuffer = "<";
            this.inTag = true;
            this.currentChar++;
            this.run();
            return;
        } else if (this.origText.charAt(this.currentChar) == ">" && this.inTag) {
            this.tagBuffer += ">";
            this.inTag = false;
            this.currentText += this.tagBuffer;
            this.currentChar++;
            this.run();
            return;
        } else if (this.inTag) {
            this.tagBuffer += this.origText.charAt(this.currentChar);
            this.currentChar++;
            this.run();
            return;
        } else if (this.origText.charAt(this.currentChar) == "&" && !this.inHTMLEntity) {
            this.HTMLEntityBuffer = "&";
            this.inHTMLEntity = true;
            this.currentChar++;
            this.run();
            return;
        } else if (this.origText.charAt(this.currentChar) == ";" && this.inHTMLEntity) {
            this.HTMLEntityBuffer += ";";
            this.inHTMLEntity = false;
            this.currentText += this.HTMLEntityBuffer;
            this.currentChar++;
            this.run();
            return;
        } else if (this.inHTMLEntity) {
            this.HTMLEntityBuffer += this.origText.charAt(this.currentChar);
            this.currentChar++;
            this.run();
            return;
        } else {
            this.currentText += this.origText.charAt(this.currentChar);
        }
        this.element.innerHTML = this.currentText;
        this.element.innerHTML += (this.currentChar < this.origText.length - 1 ? (typeof this.cursor == "function" ? this.cursor(this.currentText) : this.cursor) : "");
        this.currentChar++;
        setTimeout("document.getElementById('" + this.element.id + "').typingText.run()", this.interval);
    } else {
        this.currentText = "";
        this.currentChar = 0;
        this.running = false;
        this.finishedCallback();
    }
}
</script>



<div id="divTypingtext">
    <%--Place your text here--%>
</div>

<p id="pTypingtext">
    <%--Place your text here--%>
</p>


<input type="button" id="btnShow" onclick='TypingText.runAll();' value="Start Typing Text"/>
 



<script type="text/javascript">
  
new TypingText(document.getElementById("divTypingtext"), 100, "_", TaskCompleted1);
  
new TypingText(document.getElementById("pTypingtext"), 100, function (i) { var ar = new Array("_", " "); return " " + ar[i.length % ar.length]; }, TaskCompleted2);

function TaskCompleted1() {
    alert("Task 1 Completed")
}
function TaskCompleted2() {
    alert("Task 2 Completed")
}
</script>
Here you can also use manual cursor as you want, just to change the function of cursor some manual cursor function given below.

function (i) { var ar = new Array("_", " "); return " " + ar[i.length % ar.length]; }

Popular posts from this blog

regex - check if a string contains only alphabets c#

How to validate that input string contains only alphabets, validating that textbox contains only alphabets (letter), so here is some of the ways for doing such task. char have a property named isLetter which is for checking if character is a letter or not, or you can check by the regular expression  or you can validate your textbox through regular expression validator in asp.net. Following code demonstrating the various ways of implementation.

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -

How to validate dropdownlist in JavaScript

In this article you will see how to put validation in dropdownlist by javascript, suppose first item value of dropdownlist is 0 and text is "-Select-" just like given below and we have to validate that at least one item is selected excluding default i.e "-Select-".

Regular expression for alphanumeric with space in asp.net c#

How to validate that string contains only alphanumeric value with some spacial character and with whitespace and how to validate that user can only input alphanumeric with given special character or space in a textbox (like name fields or remarks fields). In remarks fields we don't want that user can enter anything, user can only able to enter alphanumeric with white space and some spacial character like -,. etc if you allow. Some of regular expression given below for validating alphanumeric value only, alphanumeric with whitspace only and alphanumeric with whitespace and some special characters.