Hi there!
I used the following code https://www.grampsworkbench.com/Examples/Typewriter to create a typewriter effect. I am still pretty beginner level when it comes to coding.
This is the code, which actually works perfectly:
let interval ;
let timeInterval = 500 ;
let typeStr = “I AM NINA” ;
let typeIdx ;
let htmlStr = ‘’ ;
let displayStr ;
$w . onReady ( function () {
// Get the original html string, and split it into two pieces.
// In the process, remove the separator !##!
// By saving the original html, we preserve the style of the text to display.
if ( htmlStr . length === 0 ) { // just get the original html text the first time
htmlStr = $w ( "#typewriterPage" ). html ;
}
$w ( "#typewriterPage" ). html = '' ; // don't show the original text
let strPieces = htmlStr . split ( "!##!" );
displayStr = '' ; // init string that we will "type" to
typeIdx = 0 ; // start at beginning of string we want to "type"
// Declare an interval function that will run in the background.
// This function will be triggered by the defined timeInterval.
interval = setInterval ( function () {
// Get next char from string to be typed and concatenate to the display string.
displayStr = displayStr + typeStr [ typeIdx ++];
// Make a sandwich with the display string in between the original html pieces.
$w ( "#typewriterPage" ). html = strPieces [ 0 ] + displayStr + strPieces [ 1 ];
// Stop the interval from running when we get to the last character to by "typed".
if ( typeIdx === typeStr . length ) clearInterval ( interval );
}, timeInterval );
But I would like to add the following modification:
the first line should be erased (or even partially) and then retyped with another sentence and so on:
“I AM NINA” -erases-
-typed on same spot instead- “I AM DESIGNER” -erases-
-typed on same spot instead- “I AM STRATEGIST” -erases-
Hi there, I am a complete noob when it comes to coding, I have been redirected here as I asked a similar question. In what part of this code can I use to direct it to an element, say for example “#text37”? I may be missing a few steps but the above code does not work for me
Hi Bruno - a very quick question. If I wanted 3 separate Type Writer Effects on the same page - how would I do that? Also - thanks for this! Has helped me out so much
I’ve been trying to use this code using an element ID but I get this error whenever I try to run the code, can anyone help with this? I’m a total beginner so I’m a bit stumped!
I’m using an element called #intro and I’m struggling with the code.
I know this is super late, and I’m no coder whatsoever (thank you bruno for your help!!), but if you add spaces after type strings, it will look like the type is pausing before the next string appears
Resurrecting this old post because I ran into this problem recently and needed to add a starting string and the backspace animation so I thought I’d add the updated changes here. Started with @bwprado 's code so thank you for the jumping off point!
$w.onReady(() => {
typeAndErase($w("#typerwriterText")) //Just the function call
})
function typeAndErase(textElement) {
let interval
let backspaceInterval
let timeInterval = 300
let typeStrings = ["Blaise Clair", "Website Designer", "Wix Coder"]
let stringStart = "I am " //<-- if you don't want a starting string just set this to ""
let wordIdx = 0
let typeIdx = 0
let displayStr = stringStart
let endingString = "|"
let direction = 1
textElement.text = ""
interval = setInterval(() => {
if(direction == 1) {
displayStr = displayStr + typeStrings[wordIdx][typeIdx]
typeIdx++
textElement.text = displayStr + endingString
if (typeIdx === typeStrings[wordIdx].length) {
direction = 0
}
}
else{
typeIdx--;
displayStr = displayStr.substr(0,stringStart.length + typeIdx-1);
textElement.text = displayStr + endingString;
if(typeIdx == 0) {
direction = 1
wordIdx++
displayStr = stringStart
if (wordIdx === typeStrings.length) wordIdx = 0
}
}
}, timeInterval)
}