Created
June 27, 2015 18:37
-
-
Save craigmichaelmartin/cfba075d487499a161b0 to your computer and use it in GitHub Desktop.
Vanishes the text content of elements with a certain class in some milliseconds.
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
// Vanishes the text content of elements with a certain class in some milliseconds | |
var vanishByClassName = (function() { | |
// Returns a random integer [min, max) | |
var getRandomInt = function(min, max) { | |
return Math.floor(Math.random() * (max - min)) + min; | |
}; | |
// Returns a string with a swapped out character | |
var replaceStringCharacter = function(string, index, character) { | |
return string.substring(0,index) + character + string.substring(index+1); | |
}; | |
// Returns an array of length with sequential numbers | |
var arrayOfSequentialNumbers = function(length) { | |
return Array.apply(null, {length: length}).map(Number.call, Number); | |
}; | |
// Returns a copy of an array spliced | |
var splice = function(array, index, count) { | |
array = array.slice(); | |
count !== void 0 ? array.splice(index, count) : array.splice(index, count); | |
return array; | |
}; | |
// Vanishes the text of an element at a step of a time iteration | |
var vanishElement = function(element, timeIteration, arrayOfIndexes) { | |
arrayOfIndexes = arrayOfIndexes || arrayOfSequentialNumbers(element.textContent.length); | |
var index = getRandomInt(0,arrayOfIndexes.length); | |
var spot = arrayOfIndexes[index]; | |
arrayOfIndexes.splice(index, 1); | |
element.textContent = replaceStringCharacter(element.textContent, spot, ' '); | |
if (arrayOfIndexes.length > 0) { | |
setTimeout(vanishElement.bind(null, element, timeIteration, arrayOfIndexes), timeIteration); | |
} | |
}; | |
// Vanishes the text in an array of elements simultaneously in some milliseconds | |
var vanishElements = function(elements, milliseconds) { | |
if (elements.length === 0) { | |
return; | |
} | |
vanishElement(elements[0], milliseconds/elements[0].textContent.length); | |
vanishElements(splice(elements, 0, 1), milliseconds); | |
} | |
// Vanishes the text content with a certain class in some milliseconds | |
return function(className, milliseconds) { | |
return vanishElements([].slice.call(document.getElementsByClassName(className)), milliseconds); | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment