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)
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.
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".
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.
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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With