Jump to content

Adding Typewriter Effect via Code Block

Go to solution Solved by tuanphan,

Recommended Posts

Site URL: https://www.wroteon.com/tester

I am trying to inject the code from this article into my own site using a code block. In trying to combine the codes into one and change it to h3 text, I tried the below code, which give the result you can currently see with the dark grey background (please ignore the typewriter effect on the text in the lower section as that is a different approach I am wanting to avoid using):

<h3 id="check">
	I am
	<span id="typewriter"></span>
</h3>

<script>
let data = ["Text1", "Text2","Text3","Text4"];
let len = 0;
let speed=150;
for (let i = 0; i < data.length; ++i) len += 2*data[i].length;
len += 4*data.length;
function type() {
    let p=0;
    for (let i = 0; i < data.length; ++i) {
        let x = 0;
        while (x <= data[i].length) {
            let y=x;
            setTimeout(()=>{
                document.getElementById('typewriter').innerHTML=`${data[i].substr(0,y)}`
            },p*speed)
            ++x,++p;
        }
        --x,p+=2;
        while (x >= 0) {
            let y=x;
            setTimeout(()=>{
                document.getElementById('typewriter').innerHTML=`${data[i].substr(0,y)}`
            },p*speed)
            --x,++p;
        }
    }
    setTimeout(() => {
        type();
    }, len * speed);
}

function blinkingPointer(){
    setTimeout(()=>{
        document.getElementById('check').style.borderRightColor=`red`;
    },500)  
    setTimeout(()=>{
        document.getElementById('check').style.borderRightColor='transparent'
    },1000)  
    setTimeout(()=>{
        blinkingPointer();
    },1000)

}

blinkingPointer();
type();

  #check{
    margin:2px auto;
    width: fit-content;
    border-right: 1px solid;
}
</script>

I have a feeling this has more to do with how I am injecting the code rather than the code itself. 

Thanks for any help you can give!

Link to comment
  • Replies 1
  • Views 249
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

  • Solution

This code is CSS, you can't add it into <script>

#check{
    margin:2px auto;
    width: fit-content;
    border-right: 1px solid;
}

add this under </script> line

<style>
  #check{
    margin:2px auto;
    width: fit-content;
    border-right: 1px solid;
}
</style>

 

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.