Last active
September 21, 2017 07:12
-
-
Save boazcstrike/7cd6d8f2c192aa0e70c479afde659a47 to your computer and use it in GitHub Desktop.
This is the javascript typewriter effect using arrays, without letter styles
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
<!DOCTYPE html> | |
<html lang="EN"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title></title> | |
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> | |
<!-- WARNING: Respond.js doesn't work if you view the page via file:// --> | |
<!--[if lt IE 9]> | |
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.2/html5shiv.min.js"></script> | |
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script> | |
<![endif]--> | |
<style> | |
.typewriter{ | |
white-space: nowrap; | |
overflow: hidden; | |
animation: | |
typing 4.5s, | |
blink-caret .75s step-end infinite; | |
} | |
span#blink{ | |
color:lime !important; | |
animation: blink 1s infinite; | |
} | |
@keyframes typing { | |
from { width: 0 } | |
to { width: 100% } | |
} | |
@keyframes blink{ | |
to{opacity: .0;} | |
} | |
::selection{ | |
background: black; | |
} | |
</style> | |
</head> | |
<body> | |
<h1> | |
<a href="" class="typewrite" data-period="2000" data-type='[ "Hi, Im Si.", "I am Creative.", "I Love Design.", "I Love to Develop." ]'> | |
<span class="wrap"></span> | |
</a> | |
</h1> | |
</body> | |
<script> | |
var TxtType = function(el, toRotate, period) { | |
this.toRotate = toRotate; | |
this.el = el; | |
this.loopNum = 0; | |
this.period = parseInt(period, 10) || 2000; | |
this.txt = ''; | |
this.tick(); | |
this.isDeleting = false; | |
}; | |
TxtType.prototype.tick = function() { | |
var i = this.loopNum % this.toRotate.length; | |
var fullTxt = this.toRotate[i]; | |
if (this.isDeleting) { | |
this.txt = fullTxt.substring(0, this.txt.length - 1); | |
} else { | |
this.txt = fullTxt.substring(0, this.txt.length + 1); | |
} | |
this.el.innerHTML = '<span class="wrap">'+this.txt+'</span>'; | |
var that = this; | |
var delta = 200 - Math.random() * 100; | |
if (this.isDeleting) { delta /= 2; } | |
if (!this.isDeleting && this.txt === fullTxt) { | |
delta = this.period; | |
this.isDeleting = true; | |
} else if (this.isDeleting && this.txt === '') { | |
this.isDeleting = false; | |
this.loopNum++; | |
delta = 500; | |
} | |
setTimeout(function() { | |
that.tick(); | |
}, delta); | |
}; | |
window.onload = function() { | |
var elements = document.getElementsByClassName('typewrite'); | |
for (var i=0; i<elements.length; i++) { | |
var toRotate = elements[i].getAttribute('data-type'); | |
var period = elements[i].getAttribute('data-period'); | |
if (toRotate) { | |
new TxtType(elements[i], JSON.parse(toRotate), period); | |
} | |
} | |
// INJECT CSS | |
var css = document.createElement("style"); | |
css.type = "text/css"; | |
css.innerHTML = ".typewrite > .wrap { border-right: 0.08em solid #fff}"; | |
document.body.appendChild(css); | |
}; | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment