Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typewriter-Effect for HTML with JavaScript [duplicate]

I want to do the following: I want to have a typewriter effect in HTML/JavaScript (jQuery/jQuery UI, if needed). There are tons of great examples out there on how to create a typewriter effect on a string (for example this one). I want to do something similar, but with a complete HTML string, which shouldn't be typed out, but inserted properly into the web page.

Example string:

<p>This is my <span style='color:red;'>special string</span> with an <img src="test.png"/> image !</p>

This string should be typed with a typewriter animation. The color of "special string" should be red, even while typing, and the image should appear after the word "an" and before the word "image". The problem with the solutions out there is that they insert the markup character by character into the web page, which results in an unclosed while typing the "special string" in this example. I considered parsing the string with jQuery and iterating over the array, but I have no idea how I would deal with nested tags (like p and span in this example)

like image 434
Philipp Avatar asked Mar 04 '14 18:03

Philipp


People also ask

How do you make a typewriter effect in HTML?

Note that, in order for the typewriter effect to work, we've added the following: "overflow: hidden;" and a "width: 0;" , to make sure the text content isn't revealed until the typing effect has started. "border-right: . 15em solid orange;" , to create the typewriter cursor.

How do you animate text in JavaScript?

To make an animation possible, the animated element must be animated relative to a "parent container". The container element should be created with style = "position: relative". The animation element should be created with style = "position: absolute".

How do you make a typewriter effect?

Start a New composition to your preferred size. Right-click on the timeline and choose New > New Solid; choose the color background you want. Select the Text tool and type your title in the Media Viewer; use the Character panel to adjust the size, color and font. In the Effects panel, search for the Typewriter effect.


2 Answers

I think you don't really need a plugin to do this stuff, I made a simple example:

html:

 <div id="typewriter"></div>

js:

var str = "<p>This is my <span style='color:red;'>special string</span> with an <img src='http://placehold.it/150x150'> image !</p>",
    i = 0,
    isTag,
    text;

(function type() {
    text = str.slice(0, ++i);
    if (text === str) return;

    document.getElementById('typewriter').innerHTML = text;

    var char = text.slice(-1);
    if( char === '<' ) isTag = true;
    if( char === '>' ) isTag = false;

    if (isTag) return type();
    setTimeout(type, 80);
}());

And here is the demo on jsfiddle

like image 104
Tachun Lin Avatar answered Nov 24 '22 00:11

Tachun Lin


There is a fantastic plugin available on Github, here. An example from the README looks like this:

It's nice and configurable depending on how human-like you want the output to be. A simple example looks like this:

var tw = typewriter($('.whatever')[0]).withAccuracy(90)
                                     .withMinimumSpeed(5)
                                     .withMaximumSpeed(10)
                                     .build();
like image 40
Bojangles Avatar answered Nov 23 '22 23:11

Bojangles