12

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)

Community
  • 1
  • 1
Philipp
  • 1,903
  • 2
  • 24
  • 33
  • 1
    I have reviewed the question - it is indeed a solution for my question. I have voted to close this question. – Philipp Mar 04 '14 at 20:02

3 Answers3

33

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

Tachun Lin
  • 942
  • 3
  • 11
  • 18
  • 1
    Much simpler than the solution in the duplicate. Looks great - thanks! one question though (unrelated to the question above), what does surrounding your function with (..()) do? I always wanted to know, but didn't have the right keywords to google it.. – Philipp Mar 08 '14 at 17:54
  • 1
    @PhilippFlenker It's called a self-invoked function (anonymous function), it's returning a function object. When you append () to it, it is invoked and anything in the body is executed. – Tachun Lin Mar 08 '14 at 18:32
  • is there any difference to creating a named function and executing it (except for the name, of course)? – Philipp Mar 08 '14 at 18:34
  • Any chance you can simulate part of the text being selected? I wanted to create something really similar to this effect on HubSpot home page: www.hubspot.com – Leandro Faria Jan 24 '15 at 15:04
  • i modified this a bit. can be used as a function to all HTML elements that contain text. var animateText = function(element){ var str = element.innerHTML, i = 0, text; (function type() { text = str.slice(0, ++i); element.innerHTML = text; if (text === str) return; setTimeout(type, 90); }()); } – dy_ Apr 01 '16 at 11:28
  • This is pretty nice. Would be nice to have a way that each time you call the function, it types the given string on a new line, and not replacing the old one. – Kaizokupuffball Sep 24 '16 at 13:00
  • Very nice and simple solution, kudos to you man. I created a very simple script with it that also allows for deleting some content (once, which was all I needed). http://jsfiddle.net/VG8MJ/651/ – Aart den Braber Mar 25 '17 at 23:55
6

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();
Bojangles
  • 99,427
  • 50
  • 170
  • 208
  • I've just realised this plugin is written in f****** Coffeescript, so you'll need to compile it to JS yourself. There are instructions in the readme – Bojangles Mar 04 '14 at 19:09
  • Never really dabbled with Coffee Script, but i heard good things. I think i might look into this to replace the current way i do mine, using a jTypeWritter jQuery plugin. Ill have to look at the particulars of it later. – Fallenreaper Mar 04 '14 at 19:11
  • 3
    These days I am seeing people who are writing Coffeescript instead of JS in Github repo too. And I am hearing people who are saying plugins more times than number of times they have used `var` keyword in their code :) – redV Mar 04 '14 at 19:15
  • Thanks for the plugin - it really looks cool, but doesn't solve my problem. If I feed it the string from my example, it types it out without changing the color where it should. – Philipp Mar 04 '14 at 19:45
0

I will forward you to my old website design which used it.

myWebsiteImplementsThis

It makes use of jTypeWriter.js, blink.js, and jquery.

In the source you will the code.

You would want to take a look at function start()

and that will start the login design. I actually put it on hiatus, because there are actually some minor issues IRL i am working on, but you can easily see a working example at least :)

code:

$(function(){
    start();
});
function start(){ 
    $("#start").show(); 
    setTimeout(ssh,1000);
}
function ssh(){
    $("#ssh").show().jTypeWriter({duration:2.5}); 
    setTimeout(pass,3000);
}
function pass(){
    $("#pass").show(); 
    setTimeout(...,2500);
}
/* etc etc */

Markup:

<pre id="start" style="display:none;">localhost$ <span id="ssh" style="display:none;">ssh fallenreaper@www.fallerneaper.com</span></pre>
Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • 2
    I won't down-vote, but this should realistically be; if you're going to reference an external site, at the very least place the relevant code on SO so people don't have to visit (and so it remains here for posterity). – Brad Christie Mar 04 '14 at 19:01
  • I will Link code then – Fallenreaper Mar 04 '14 at 19:02
  • The idea behind your example is to incorporate a function for each html tag. I am looking for something more flexible, which can deal with more or less dynamic text input (The idea is that the server sends preformatted strings to a client, which types it out to the user) – Philipp Mar 04 '14 at 19:21
  • my example makes use of Chaining a bunch of them together to give the illusion of a console. I am not sure if that is the idea you were thinking of doing or not, but by obtaining text from a server, you could easily say: `$("").append(_new_text_here).hide().appendTo(_target_).show().jTypeWriter({some:"values", would:"go", here:"."})` – Fallenreaper Mar 04 '14 at 19:24
  • Hmm, I can't find the most recent version of jTypeWriter.js for evaluation - all the links I find point to nowhere... – Philipp Mar 04 '14 at 19:51
  • The only version i can see is located at: http://pastebin.com/W85XGWcV – Fallenreaper Mar 04 '14 at 19:59