Skip to content

Instantly share code, notes, and snippets.

@ryanpcmcquen
Last active October 4, 2023 18:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanpcmcquen/4504444fb15df52f24e71efd0bb7a13b to your computer and use it in GitHub Desktop.
Save ryanpcmcquen/4504444fb15df52f24e71efd0bb7a13b to your computer and use it in GitHub Desktop.
Old school typing with new school code.
.input {
display: none;
}
.output {
display: flex;
align-items: center;
justify-content: center;
color: #000000;
font-family: monospace;
font-size: 105%;
height: 5vh;
}
.output::after {
content: "|";
font-size: 110%;
top: -1px;
position: relative;
animation: blink 0.6s steps(3, start) infinite;
}
@keyframes blink {
to {
visibility: hidden;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="typewriter.css">
</head>
<body>
<div>
<span class="input">
What up dog?
</span>
<span class="output"></span>
</div>
<script src="typewriter.js"></script>
</body>
</html>
document.addEventListener('DOMContentLoaded', () => {
const typeWriter = (obj) => {
const firstIndex = obj.firstIndex
const speed = obj.speed
const inputElem = obj.inputElem
const outputElem = obj.outputElem
const input = document.querySelector(inputElem).textContent
const outputNode = document.querySelector(outputElem)
if (firstIndex === 0) {
outputNode.textContent = ''
} else {
outputNode.textContent += input.charAt(firstIndex)
}
window.setTimeout(() => {
if (firstIndex < input.length - 1) {
typeWriter({
firstIndex: firstIndex + 1,
speed: speed,
inputElem: inputElem,
outputElem: outputElem
})
} else {
typeWriter({
firstIndex: 0,
speed: speed,
inputElem: inputElem,
outputElem: outputElem
})
}
}, speed)
}
typeWriter({
firstIndex: 0,
speed: 75,
inputElem: ".input",
outputElem: ".output"
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment