Skip to content

Instantly share code, notes, and snippets.

@curiouslychase
Last active December 21, 2015 13:29
Show Gist options
  • Save curiouslychase/6313422 to your computer and use it in GitHub Desktop.
Save curiouslychase/6313422 to your computer and use it in GitHub Desktop.
Hacky Type To For a Hackathon Project
<!DOCTYPE html>
<html>
<head>
<title>TypeTo</title>
<link href='http://fonts.googleapis.com/css?family=Shadows+Into+Light' rel='stylesheet' type='text/css'>
<style type="text/css">
input, div {
font-size: 36px;
font-weight: bold;
}
input {
font-family: "Shadows Into Light", cursive;
padding: 15px;
color: #fff;
background: #000;
opacity: .7;
outline: none;
border: none;
}
#replacer {
background: #000;
color: #fff;
}
#focus_bar {
font-size: 48px;
font-weight: normal;
display: inline-block;
position: relative;
top: 5px;
}
#focus_bar.hide, #focus_bar.perm {
color: #fff;
}
.desc {
padding: 1em 0;
font-size: 16px;
}
</style>
</head>
<body>
<!-- CODE NECESSARY FOR HTML -->
<div id="iama">i am a <input id="type-to-container" /><span id="replacer"></span></div>
<!-- END CODE NECESSARY FOR HTML -->
<h1>"iama" demo</h1>
<div class="desc">
Just a bunch of setInterval and setTimeouts
</div>
<a href="https://gist.github.com/realchaseadams/6313422">A Gist on Github</a>
<script type="text/javascript" src="components/jquery/jquery.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
(function(){
var doc = document;
var type_to_container = $(doc.getElementById("type-to-container"));
var userIn = false;
type_to_container.focus();
type_to_container.on("click", function() {
userIn = true;
$(this).val("");
}).on("blur", function() {
if(this.value === "") {
userIn = false;
typeToNext();
}
});
var sample_types = [
"mother",
"hiker",
"climber",
"fashionista"
];
var flag = 0;
var place = 0;
var replacementText = [];
var intervalTime = 3500;
var typeToNext = function() {
if(userIn) return false;
if(flag == sample_types.length) {
flag = 0;
}
type_to_container.html("");
place = 0;
typeTo(type_to_container, sample_types[flag]);
flag++;
};
typeToNext();
function typeTo(type_to_container, sample_type) {
if(userIn) return false;
var len = sample_type.length;
var typeTime = intervalTime;
var perTime = Math.floor((intervalTime/len)-200);
var typeALetter = setInterval(function() {
if(place === len) {
clearInterval(typeALetter);
selectText(type_to_container, sample_type);
} else {
typeLetter(type_to_container, sample_type);
}
}, 200);
}
function typeLetter(type_to_container, sample_type) {
if(userIn) return false;
doc.getElementById("type-to-container").value += sample_type[place];
place++;
}
function selectText(type_to_container, sample_type) {
// var iHTML = type_to_container.context.innerHTML.split("");
$(doc.getElementById("type-to-container")).select();
setTimeout(function(){
$(doc.getElementById("type-to-container")).val("");
setTimeout(function(){
typeToNext();
}, 900);
}, 500);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment