JSFiddle - React, Tailwind, and code Playground
HTML
<div class="intro-section" id="intro-1">
<div class="intro-content">
<p></p>
</div>
</div>
CSS
.intro-content .large {
font-size: 2.25em;
font-family: sans-serif;
}
.typewriter::after {
content: "";
height: 1.5em;
width: 4px;
position: absolute;
bottom: 0;
right: -10px;
background-color: transparent;
animation: blink-caret 0.65s step-end infinite;
z-index: inherit;
display: block;
}
@keyframes blink-caret {
from,
to {
background-color: blue;
}
50% {
background-color: transparent;
}
}
JavaScript
let sections = [
{
el: document.getElementById("intro-1"),
content: [
{
type: "string",
value: "This is a test string with many letters in it.",
},
{
type: "sup",
value: `<sup><a href="https://www.nature.com/articles/s41591-024-03173-6.pdf" target="_blank">[1]</a></sup>`,
},
],
},
]
const sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms))
}
function addSectionContent(section) {
let p = document.querySelector(`#intro-${section} .intro-content p`)
p.classList.add("large", "typewriter")
sections[section - 1].content.forEach((c) => {
switch (c.type) {
case "string":
c.value.split("").forEach((letter) => {
sleep(1000).then(() => {
console.log(new Date().getTime())
p.innerHTML += letter
})
})
break
case "sup":
p.innerHTML += c.value
break
}
})
p.classList.remove("typewriter")
}
addSectionContent(1);