tylerperkins.xyz/themes/clortox/static/js/scramble.js

30 lines
874 B
JavaScript
Raw Normal View History

2023-07-15 22:44:31 +00:00
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
window.onload = function() {
document.querySelectorAll(".scramble").forEach(item => {
item.addEventListener("mouseover", scramble);
});
}
function scramble(event) {
let iterations = 0;
const originalValue = event.target.dataset.value;
let interval = setInterval(() => {
event.target.innerText = event.target.innerText.split("")
.map((letter, index) => {
if(index < iterations){
return originalValue[index];
}
return letters[Math.floor(Math.random() * letters.length)];
}).join("")
iterations += 1 / 3;
if(iterations > originalValue.length){
clearInterval(interval);
event.target.innerText = originalValue;
}
}, 30);
}