0

I found the following code:

var index = 0;
var text = 'Hardcode';
// Here you can put in the text you want to make it type.
function type()
{
document.getElementById('intro1').innerHTML += text.charAt(index);
index += 1;
var t = setTimeout('type()',200);
// The time taken for each character here is 100ms. You can change it if you want.
}

The problem is that the text only applies to a few attributes in my CSS (marked with //):

#intro1 {
font-family: 'Press Start 2P', cursive; //OK
font-size: 80px; //OK
display: block; //?
text-align: center; //OK
margin-bottom: auto;
margin-top: auto;
top: 50%;
}

I'm trying to get the text appear to the center of the page, but JavaScript ignores my placing. It worked when I typed the text directly in to the div in HTML, but when I made JavaScript do the dirty work, it messed it up.

Also, how can I add an delay to the animation (starts after interval)?

Help, please?

Claudio
  • 884
  • 1
  • 15
  • 31

1 Answers1

0

I changed the way you call your type() function in setInterval. Hopefully this answers your question.

http://jsfiddle.net/fenderistic/eHGK8/

JS

var index = 0;
var text = 'Hardcode';
// Here you can put in the text you want to make it type.
var f = setTimeout(function () {
    type()
}, 1000);

function type() {
   document.getElementById('intro1').innerHTML += text.charAt(index);
   index += 1;
   var t = setTimeout(function () {
       type()
   }, 200);
   // The time taken for each character here is 100ms. You can change it if you want.
}

CSS

html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
display: table
}
#intro1 {
display: table-cell;
text-align: center;
vertical-align: middle;
font-family:'Press Start 2P', cursive;
font-size: 80px;
}
em_
  • 2,134
  • 2
  • 24
  • 39
  • But it still doesn't get centered. That was the main question. – Claudio Jan 07 '14 at 18:11
  • It is getting centered in the main window for me, was that not your question? – em_ Jan 07 '14 at 18:13
  • It's horizontally centered in Fiddle, but I'd need it vertical as well. – Claudio Jan 07 '14 at 18:18
  • #intro1 { font-family:'Press Start 2P', cursive; font-size: 80px; display: block; text-align: center; margin-bottom: auto; margin-top: auto; top: 50%; left: 35%; position: absolute; } – Cam Jan 07 '14 at 18:19
  • It works, tho it's a bit tricky, as for me it's left: 30%... Wonder how it look on smaller/bigger screens. – Claudio Jan 07 '14 at 18:34
  • @Claudio I updated the CSS, it corresponds to an answer I found here http://stackoverflow.com/a/14124125/2435402 – em_ Jan 07 '14 at 18:44
  • And where's the delay now? I mean, I want the animation to start after a second, not immediately. – Claudio Jan 08 '14 at 07:02
  • Ok, @Claudio, I made it so that the animation starts after a second (1000ms), then it will output a letter every 200ms. Is that what you wanted? – em_ Jan 08 '14 at 14:19