Created
September 21, 2017 06:29
-
-
Save boazcstrike/afeb3983f88fceb41d7700092830c2d2 to your computer and use it in GitHub Desktop.
coding style or longer text typewriting effect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<pre id="typewriter"> | |
<span class="var-highlight">var</span> object = { | |
name: <span class="string-highlight">'Foo'</span>, | |
type: <span class="string-highlight">'Bar'</span>, | |
location: <span class="string-highlight">'Earth'</span>, | |
properties:[<span class="string-highlight">'Javascript'</span>, | |
<span class="string-highlight">'HTML'</span>, | |
<span class="string-highlight">'CSS'</span>]; | |
}; | |
</pre> | |
<style> | |
.var-highlight{ | |
color: #C0AD60; | |
} | |
.string-highlight{ | |
color: rgba(253, 149, 90, 0.8); | |
} | |
#typewriter{ | |
font-size: 2em; | |
margin: 0; | |
font-family: "Courier New"; | |
&:after{ | |
content: "|"; | |
animation: blink 500ms linear infinite alternate; | |
} | |
} | |
@-webkit-keyframes blink{ | |
0%{opacity: 0;} | |
100%{opacity: 1;} | |
} | |
@-moz-keyframes blink{ | |
0%{opacity: 0;} | |
100%{opacity: 1;} | |
} | |
@keyframes blink{ | |
0%{opacity: 0;} | |
100%{opacity: 1;} | |
} | |
</style> | |
<script> | |
function setupTypewriter(t) { | |
var HTML = t.innerHTML; | |
t.innerHTML = ""; | |
var cursorPosition = 0, | |
tag = "", | |
writingTag = false, | |
tagOpen = false, | |
typeSpeed = 100, | |
tempTypeSpeed = 0; | |
var type = function() { | |
if (writingTag === true) { | |
tag += HTML[cursorPosition]; | |
} | |
if (HTML[cursorPosition] === "<") { | |
tempTypeSpeed = 0; | |
if (tagOpen) { | |
tagOpen = false; | |
writingTag = true; | |
} else { | |
tag = ""; | |
tagOpen = true; | |
writingTag = true; | |
tag += HTML[cursorPosition]; | |
} | |
} | |
if (!writingTag && tagOpen) { | |
tag.innerHTML += HTML[cursorPosition]; | |
} | |
if (!writingTag && !tagOpen) { | |
if (HTML[cursorPosition] === " ") { | |
tempTypeSpeed = 0; | |
} | |
else { | |
tempTypeSpeed = (Math.random() * typeSpeed) + 50; | |
} | |
t.innerHTML += HTML[cursorPosition]; | |
} | |
if (writingTag === true && HTML[cursorPosition] === ">") { | |
tempTypeSpeed = (Math.random() * typeSpeed) + 50; | |
writingTag = false; | |
if (tagOpen) { | |
var newSpan = document.createElement("span"); | |
t.appendChild(newSpan); | |
newSpan.innerHTML = tag; | |
tag = newSpan.firstChild; | |
} | |
} | |
cursorPosition += 1; | |
if (cursorPosition < HTML.length - 1) { | |
setTimeout(type, tempTypeSpeed); | |
} | |
}; | |
return { | |
type: type | |
}; | |
} | |
var typer = document.getElementById('typewriter'); | |
typewriter = setupTypewriter(typewriter); | |
typewriter.type(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment