Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Typewriter JQuery Effect?

So I was wondering if anyone knew how to make JQuery take the text of s html element and make it look like it's being typed out? for example if I had a h1 like this

<h1>Jack's Blog</h1>

is there a way to make jquery make the "Jack's blog" text appear to type itself out?

Thank's for any and all help!

Do you mean make each letter appear one after the other like how it comes out on a typewriter? Or do you mean with full sound effects?

I just want the letters to appear one after another :)

2 Answers

This is an interesting effect that can be achieved via different methods.

One way is to use Typed.js - this is a Jquery plugin that allows you to achieve the desired typewriter effect.

Examples of the plugin in action can be seen here:

http://patrickelhage.com/ https://commando.io/

Installation

Download typed.js from Github and add the script tag to your HTML, make sure you have jquery included as well.

<script src="jquery.js"></script>
<script src="typed.js"></script>

Initializing the plugin

Add the following code above the closing body tag on the page to activate the plugin.

<script>
    $(function(){
        $(".element").typed({
            strings: ["First sentence.", "Second sentence."],
            typeSpeed: 0
        });
    });
</script>

The HTML

Looking back at the above code snippet, we can see that the class used in the function was .element - you can make it anything you want. Now we move on and add the class to a span element.

<span class="element">
<p>Write any text you want</p>
<p>Watch it type itself out</p>
</span>

To see more options and instructions, refer to the Github page and check it out.

A CSS-only approach is also possible

The following pen illustrates this and shows how its done

http://codepen.io/Mrshcom/pen/uHEpv

Best of Luck and let me know if you're still having problems.

Typed.js looks great, Taki. Didn't know it existed. Gonna have to add that one to my list. :-)

Anytime man, glad to help : )

To make the letters appear one after another, you're going to want to take a look at creating a custom effect using the animate method.

I'm going to explain this the simplest way I can conceptually, then you can tinker with it and perfect it if you want to turn it into reusable code.

The easiest way I can think to do this is to take each letter in the H1 tag and wrap it in it's own span with an ID set to a number.

For example, "ROY" would be:

<h1>
     <span id="1">R</span> <span id="2">O</span><span id="3">Y</span>
</h1>

You would use CSS to set the initial opacity of the spans to 0. Setting the opacity to 0 rather than setting the display to none holds the space the element is in rather than collapsing the HTML. Display none actually removes the element from the document flow.

Then you'll use the animate method to change the opacity of the first letter from 0 to 1.

You'll have to daisy chain the letters together by passing the function that animates the next letter into the complete parameter of the animate method. If you don't the animations will all run asynchronously.

I'll leave you to work out the details, but that's what you're doing on the conceptual level.