Created
May 14, 2025 12:14
-
-
Save IntegralPilot/103860b7296c9d47ae82394979aeafa0 to your computer and use it in GitHub Desktop.
Hacked Text Effect - JavaScript
This file contains hidden or 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
| // Makes a H1 element have a "hacked" effect on hover. | |
| const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
| let interval = null; | |
| document.querySelector("h1").onmouseover = event => { | |
| let iteration = 0; | |
| const targetValue = event.target.dataset.value; | |
| clearInterval(interval); | |
| interval = setInterval(() => { | |
| const currentTextInDOM = event.target.innerText; | |
| const processingLength = Math.max(currentTextInDOM.length, targetValue.length); | |
| event.target.innerText = Array.from({ length: processingLength }) | |
| .map((_, index) => { | |
| if (index < iteration) { | |
| return targetValue[index] || ""; | |
| } | |
| return letters[Math.floor(Math.random() * 26)]; | |
| }) | |
| .join(""); | |
| if (iteration >= targetValue.length) { | |
| clearInterval(interval); | |
| event.target.innerText = targetValue; | |
| } | |
| iteration += 1 / 3; | |
| }, 30); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment