I'm trying make an animation as if I was typing. To achieve this I'm using CSS animation 'steps'.
The animation itself works just fine. However, if I want to animate multiple lines of text, they all start playing at the same time. Wich isn't giving me the desired effect. (Tried using <br>
in a single <h1>
, wich cut off the text, but again started the animations simultaniously.)
To counter this, I put the next line of text in an <h2>
and set an animation-delay for every line of text. Wich works, but the text is visible before the animation starts.
I want the text to be hidden until the animation starts playing, to really get that 'live typing' effect.
Anyone got any ideas on how I can achieve this?
HTML
<div class="content">
<h1>Hi there! My name is Jeff.</h1>
<h2>And I create cool stuff.</h2>
</div>
CSS
.content h1 {
background:white;
opacity:0.7;
white-space:nowrap;
overflow:hidden;
border-right: 3px solid black;
-webkit-animation: typing 2s steps(26, end),
blink-caret 1s step-end 2s;
}
.content h2 {
background:white;
opacity:0.7;
white-space:nowrap;
overflow:hidden;
border-right: 3px solid black;
-webkit-animation: typing 2s steps(26, end),
blink-caret 1s step-end infinite;
-webkit-animation-delay:3s;
}
@-webkit-keyframes typing {
from { width: 0; }
to { width:400px; }
}
@-webkit-keyframes blink-caret {
from, to { border-color: transparent }
50% { border-color: black }
}
Thanks in advance.
The simplest solution is to add:
to your
h2
(with the necessary prefixes). That way, you aren't setting it to a zero width outside of your animation, so browsers that don't support this CSS will display the heading (which I guess is what you're after). See this fiddle.The animation-fill-mode:
Setting it to
both
in this instance means that yourh2
will have a width of0
before it starts executing, and a width of400px
after.As the comments already include a solution, perhaps this might be another way of doing it - by using timeouts and setting
visibility: hidden
at the beginning (For simplification I just used jQuery to set the visiblitiy).Include the following CSS rule:
As JavaScript you would have:
See the jsFiddle
Not quite the OP's question, but in case someone else finds this useful:
I wanted to be able to typing-animate a pararaph of text, a single
<p>
tag which might contain text that would wrap, and produce an unknown number of actual lines. Applying a simple linear animation to thep
tag itself wouldn't work, so instead, I took the approach of having several "hider" elements that would cover the paragraph of text, each one line high, and then I would animate each of those so they would shrink away, reveal characters from the line of text beneath them.The HTML looks like this:
You need a container, and position the
.typing
element and the.hiders
usingabsolute
so that they're on top of each other:And the animation gets applied to each
p
inside the.hiders
:Here's the final fiddle:
https://jsfiddle.net/hjwp/514cLzxn/
Original credit for inspiration: Lea Verou