Teechkidz's picture
Let’s make the accessible counting blocks
6673e84 verified
Raw History Blame Contribute Delete
2.44 kB
document.addEventListener('DOMContentLoaded', () => {
let count = 0;
const maxCount = 100;
const counterDisplay = document.getElementById('counter-display');
const progressBar = document.getElementById('progress-bar');
const progressText = document.getElementById('progress-text');
const speakBtn = document.getElementById('speak-btn');
// Get all counter blocks
const counterBlocks = document.querySelectorAll('.counter-block');
// Function to update counter display
function updateCounter() {
counterDisplay.textContent = count;
// Update progress
const progress = Math.min((count / maxCount) * 100, 100);
progressBar.style.width = `${progress}%`;
progressText.textContent = `${Math.round(progress)}%`;
// Visual feedback
if (count >= maxCount) {
counterDisplay.classList.add('animate-bounce');
setTimeout(() => {
counterDisplay.classList.remove('animate-bounce');
}, 1000);
}
}
// Add event listeners to counter blocks
counterBlocks.forEach(block => {
block.addEventListener('click', () => {
const action = block.textContent;
switch(action) {
case '+1':
count += 1;
break;
case '+5':
count += 5;
break;
case '+10':
count += 10;
break;
case '-1':
count = Math.max(0, count - 1);
break;
case 'Reset':
count = 0;
break;
}
updateCounter();
// Play click sound for accessibility
const clickSound = new Audio('https://assets.mixkit.co/sfx/preview/mixkit-arcade-game-jump-coin-216.mp3');
clickSound.volume = 0.3;
clickSound.play();
});
});
// Speak current count
speakBtn.addEventListener('click', () => {
const speech = new SpeechSynthesisUtterance();
speech.text = `The current count is ${count}`;
speech.volume = 1;
speech.rate = 0.9;
speech.pitch = 1;
window.speechSynthesis.speak(speech);
});
// Initialize
updateCounter();
});