auto type - remove and highlight
by hlmurray91
HTML
<div id="js-typing"></div>
CSS
.removing{ background: yellow; }
.yellow{ background: yellow; }
JavaScript
// had to break away from. error after first removal.
var typing = function(string){
var count = 0;
var container = document.getElementById("js-typing");
var length = string.length;
var newString = "";
var timer;
var removeTimer;
timer = setInterval(function(){
if(count < length){
addLetter();
} else{
removeLetter([2,4,7,9]);
clearInterval(timer);
}
}, 50);
// addLetters
function addLetter(){
newString = newString + string.charAt(count);
container.innerHTML = newString;
count++;
}
// remove letter
function removeLetter(removeList){
var letterTimer;
var letterCount = 0;
letterTimer = setInterval(function(){
if(letterCount < removeList.length){
remove(removeList[letterCount]);
} else{
highLight(11);
clearInterval(letterTimer);
}
letterCount++;
}, 600);
function remove(char){
var array;
var chosen;
var beforeChosen;
var afterChosen;
var aTimer;
chooseLetter();
postLetter();
function chooseLetter(){
array = newString.split("");
chosen = array[char];
beforeChosen = array.splice(0, char).join("");
afterChosen = array.splice(1, array.length).join("");
}
function postLetter(){
container.innerHTML = beforeChosen + "<span class='removing'>" + chosen + "</span>" + afterChosen;
newString = beforeChosen + afterChosen;
aTimer = setTimeout(function(){
container.innerHTML = beforeChosen + afterChosen;
clearTimeout(aTimer);
}, 400);
}
}
}
function...