|
1 | 1 | document.addEventListener('DOMContentLoaded', function() { |
| 2 | + const container = document.querySelector('.typing-container'); |
2 | 3 | const lines = document.querySelectorAll('.code-line'); |
3 | | - const cursor = document.querySelector('.typing-cursor'); |
| 4 | + |
| 5 | + // Create cursor element |
| 6 | + const cursor = document.createElement('div'); |
| 7 | + cursor.className = 'typing-cursor'; |
| 8 | + container.appendChild(cursor); |
| 9 | + |
4 | 10 | let currentLine = 0; |
5 | | - |
| 11 | + |
6 | 12 | function updateCursorPosition() { |
7 | | - const activeLine = document.querySelector('.code-line.typing'); |
| 13 | + const activeLine = lines[currentLine]; |
8 | 14 | if (activeLine) { |
9 | 15 | const lineRect = activeLine.getBoundingClientRect(); |
10 | | - const containerRect = document.querySelector('.typing-container').getBoundingClientRect(); |
| 16 | + const containerRect = container.getBoundingClientRect(); |
| 17 | + cursor.style.left = `${activeLine.offsetWidth}px`; |
11 | 18 | cursor.style.top = `${lineRect.top - containerRect.top}px`; |
12 | | - cursor.style.left = `${lineRect.left - containerRect.left + activeLine.offsetWidth}px`; |
| 19 | + |
| 20 | + // Change cursor color for output line |
| 21 | + if (activeLine.classList.contains('code-output')) { |
| 22 | + cursor.classList.add('at-output'); |
| 23 | + } else { |
| 24 | + cursor.classList.remove('at-output'); |
| 25 | + } |
13 | 26 | } |
14 | 27 | } |
15 | 28 |
|
16 | 29 | function typeLine() { |
17 | 30 | if (currentLine < lines.length) { |
18 | | - lines[currentLine].classList.add('typing'); |
19 | | - |
20 | | - const typingDuration = 2000; // 2 seconds per line |
21 | | - const lineDelay = 500; // 0.5 second delay between lines |
22 | | - |
23 | | - // Update cursor position during typing |
| 31 | + // Start cursor animation for current line |
24 | 32 | const cursorInterval = setInterval(updateCursorPosition, 50); |
25 | 33 |
|
| 34 | + // After typing animation completes |
26 | 35 | setTimeout(() => { |
27 | 36 | clearInterval(cursorInterval); |
28 | 37 | currentLine++; |
29 | 38 | if (currentLine < lines.length) { |
30 | | - setTimeout(typeLine, lineDelay); |
| 39 | + setTimeout(typeLine, 500); // Delay before next line |
31 | 40 | } |
32 | | - }, typingDuration); |
| 41 | + }, 2000); // Match with CSS animation duration |
33 | 42 | } |
34 | 43 | } |
35 | 44 |
|
36 | | - // Start typing |
37 | | - typeLine(); |
| 45 | + // Start typing after a short delay |
| 46 | + setTimeout(typeLine, 500); |
38 | 47 | }); |
0 commit comments