<div class="container">
<p class="textAbove">I am </p>
<p class="typeText"></p>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html, body {
width: 100%;
}
.container {
width: 100%;
height: 100vh;
display: flex;
background-image: url("https://images.unsplash.com/photo-1522071820081-009f0129c71c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=750&q=80");
flex-flow: column;
justify-content: center;
align-items: center;
}
p {
font-size: xx-large;
color: white;
}
.typeText::after {
content: "|";
/* animation: blink 1s step-end infinite; */
animation: none;
}
.showAnim.typeText::after {
animation: blink 1s step-end infinite;
}
@keyframes blink {
0%, 100% {opacity: 1;}
50% {opacity: 0;}
}
var typeText = document.querySelector(".typeText")
var textToBeTyped = "a software engineer"
var textToBeTypedArr = ["a software engineer", "a warlord", "a king", "a peasant"]
var index = 0, isAdding = true, textToBeTypedIndex = 0
function playAnim() {
setTimeout(function () {
// set the text of typeText to a substring of the text to be typed using index.
typeText.innerText = textToBeTypedArr[textToBeTypedIndex].slice(0, index)
if (isAdding) {
// adding text
if (index > textToBeTypedArr[textToBeTypedIndex].length) {
// no more text to add
isAdding = false
typeText.classList.add("showAnim")
//break: wait 2s before playing again
setTimeout(function () {
typeText.classList.remove("showAnim")
playAnim()
}, 2000)
return
} else {
// increment index by 1
index++
}
} else {
// removing text
if (index === 0) {
// no more text to remove
isAdding = true
//switch to next text in text array
textToBeTypedIndex = (textToBeTypedIndex + 1) % textToBeTypedArr.length
} else {
// decrement index by 1
index--
}
}
// call itself
playAnim()
}, isAdding ? 120 : 60)
}
// start animation
playAnim()
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.