Skip to content

Commit 332d386

Browse files
authored
Create script.js
Create separate .js file for the animation function JavaScript sample code
1 parent 73b6f25 commit 332d386

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

script.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Create Our Animation Main Function - animateText by id
2+
function animateText(id) {
3+
const textElement = document.getElementById(id);
4+
const text = textElement.innerText;
5+
6+
// Clear the original text content
7+
textElement.innerHTML = '';
8+
9+
for (let i = 0; i < text.length; i++) {
10+
const span = document.createElement('span');
11+
span.innerText = text[i];
12+
// Adjust the delay as needed - example - ${(i + 2) * 1}s
13+
span.style.animationDelay = `${(i + 0.01) * 0.01}s`;
14+
textElement.appendChild(span);
15+
16+
// Add animationend event listener to the last created span
17+
if (i === text.length - 1) {
18+
span.addEventListener('animationend', () => {
19+
// Remove individual spans
20+
textElement.innerHTML = '';
21+
22+
// Create a single span with the entire text
23+
// When Animation End Remove all span apended with the textElement.appendChild(span);
24+
25+
const fullTextSpan = document.createElement('span');
26+
// Adding New ID for the span after the span animations been removed after the end of the animation
27+
fullTextSpan.setAttribute("id", `endanim-${id}`);
28+
// Adding New CSS clas for the span after the span animations been removed after the end of the animation
29+
fullTextSpan.setAttribute("class", `endanim-${id}`);
30+
fullTextSpan.innerText = text;
31+
textElement.appendChild(fullTextSpan);
32+
});
33+
}
34+
}
35+
}
36+
37+
// Call the function for each div id
38+
animateText('animTextOne');
39+
animateText('animTextTwo');
40+
animateText('animTextThree');

0 commit comments

Comments
 (0)