Back
Discover the creation of a sound-responsive toggle button that transforms appearance with melodious transitions as it toggles between tranquil and invigorating states.
<div class="melody-container">
<button onclick="toggleMelody()" class="mood-button" aria-label="Toggle melodic mood">
<span class="mood-state">Calm</span>
</button>
<audio id="moodSound" aria-hidden="true">
<source src="https://web.archive.org/meadow-calm.mp3" type="audio/mpeg">
<source src="https://web.archive.org/meadow-vibrant.mp3" type="audio/mpeg">
</audio>
</div>
The main element serves as a container that keeps our button and audio element centered on the page. The button is our interactive element with some accessible features, like an ARIA label, that describes its function.
.melody-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.mood-button {
border: none;
padding: 15px 30px;
border-radius: 50px;
color: white;
font-size: 18px;
font-weight: bold;
transition: background 0.5s, transform 0.3s, box-shadow 0.3s;
cursor: pointer;
background: linear-gradient(135deg, rgba(135,206,235,0.9), rgba(64,224,208,0.9));
box-shadow: 0 0 10px rgba(135,206,235,0.6);
}
.mood-button:focus {
outline: none;
box-shadow: 0 0 12px rgba(255,255,255,0.8);
}
.vibrant-mood {
transform: scale(1.1);
box-shadow: 0 0 15px rgba(255,69,0,0.7), 0 0 20px rgba(255,140,0,0.7);
}
Even the button’s focus state has been styled to ensure an easily noticeable outline, improving accessibility by enhancing the button’s visibility when navigated via keyboard.
let isCalm = true;
function toggleMelody() {
const button = document.querySelector('.mood-button');
const stateText = document.querySelector('.mood-state');
const moodSound = document.getElementById('moodSound');
isCalm = !isCalm;
if (isCalm) {
button.style.background = "linear-gradient(135deg, rgba(135,206,235,0.9), rgba(64,224,208,0.9))";
stateText.textContent = "Calm";
button.classList.remove('vibrant-mood');
moodSound.src = "https://web.archive.org/meadow-calm.mp3";
} else {
button.style.background = "linear-gradient(135deg, rgba(255,69,0,0.9), rgba(255,140,0,0.9))";
stateText.textContent = "Vibrant";
button.classList.add('vibrant-mood');
moodSound.src = "https://web.archive.org/meadow-vibrant.mp3";
}
moodSound.play();
}
This function toggles the state by tracking a boolean value, applying appropriate styles and playing corresponding audio clips. It skillfully combines colors and sounds to create an engaging and experimental user experience.<div class="melody-container">
<button onclick="toggleMelody()" class="mood-button" aria-label="Toggle melodic mood">
<span class="mood-state">Calm</span>
</button>
<audio id="moodSound" aria-hidden="true">
<source src="https://web.archive.org/meadow-calm.mp3" type="audio/mpeg">
<source src="https://web.archive.org/meadow-vibrant.mp3" type="audio/mpeg">
</audio>
</div>
<script>
let isCalm = true;
function toggleMelody() {
const button = document.querySelector('.mood-button');
const stateText = document.querySelector('.mood-state');
const moodSound = document.getElementById('moodSound');
isCalm = !isCalm;
if (isCalm) {
button.style.background = "linear-gradient(135deg, rgba(135,206,235,0.9), rgba(64,224,208,0.9))";
stateText.textContent = "Calm";
button.classList.remove('vibrant-mood');
moodSound.src = "https://web.archive.org/meadow-calm.mp3";
} else {
button.style.background = "linear-gradient(135deg, rgba(255,69,0,0.9), rgba(255,140,0,0.9))";
stateText.textContent = "Vibrant";
button.classList.add('vibrant-mood');
moodSound.src = "https://web.archive.org/meadow-vibrant.mp3";
}
moodSound.play();
}
</script>
<style>
.melody-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.mood-button {
border: none;
padding: 15px 30px;
border-radius: 50px;
color: white;
font-size: 18px;
font-weight: bold;
transition: background 0.5s, transform 0.3s, box-shadow 0.3s;
cursor: pointer;
background: linear-gradient(135deg, rgba(135,206,235,0.9), rgba(64,224,208,0.9));
box-shadow: 0 0 10px rgba(135,206,235,0.6);
}
.mood-button:focus {
outline: none;
box-shadow: 0 0 12px rgba(255,255,255,0.8);
}
.vibrant-mood {
transform: scale(1.1);
box-shadow: 0 0 15px rgba(255,69,0,0.7), 0 0 20px rgba(255,140,0,0.7);
}
</style>
Thank you for reading this article.
Comments