Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const textInput = document.getElementById('textInput');
+ const startButton = document.getElementById('startReading');
+ const previousWords = document.getElementById('previousWords');
+ const currentWord = document.getElementById('currentWord');
+
+ let words = [];
+ let currentIndex = 0;
+ let isReading = false;
+ let lastScrollPosition = window.scrollY;
+ let scrollThreshold = 20;
+
+ startButton.addEventListener('click', () => {
+ const text = textInput.value.trim();
+ if (text) {
+ words = text.split(/\s+/).filter(word => word.length > 0);
+ currentIndex = 0;
+ isReading = true;
+ textInput.style.display = 'none';
+ startButton.style.display = 'none';
+ updateDisplay();
+ }
+ });
+
+ function updateDisplay() {
+ if (currentIndex >= 0 && currentIndex < words.length) {
+ previousWords.innerHTML = words.slice(Math.max(0, currentIndex - 3), currentIndex).join(' ');
+ currentWord.textContent = words[currentIndex];
+ }
+ }
+
+ window.addEventListener('scroll', (e) => {
+ if (!isReading) return;
+
+ const currentScrollPosition = window.scrollY;
+ const scrollDifference = currentScrollPosition - lastScrollPosition;
+
+ if (Math.abs(scrollDifference) >= scrollThreshold) {
+ if (scrollDifference > 0 && currentIndex < words.length - 1) {
+ currentIndex++;
+ } else if (scrollDifference < 0 && currentIndex > 0) {
+ currentIndex--;
+ }
+ updateDisplay();
+ lastScrollPosition = currentScrollPosition;
+ }
+ });
+
+ // Mobile touch support
+ let touchStartY = 0;
+
+ window.addEventListener('touchstart', (e) => {
+ touchStartY = e.touches[0].clientY;
+ });
+
+ window.addEventListener('touchmove', (e) => {
+ if (!isReading) return;
+
+ const touchCurrentY = e.touches[0].clientY;
+ const touchDifference = touchStartY - touchCurrentY;
+
+ if (Math.abs(touchDifference) >= scrollThreshold) {
+ if (touchDifference > 0 && currentIndex < words.length - 1) {
+ currentIndex++;
+ } else if (touchDifference < 0 && currentIndex > 0) {
+ currentIndex--;
+ }
+ updateDisplay();
+ touchStartY = touchCurrentY;
+ }
+ });
+ });