问题
I was creating a website, I put background music, and my problem is I dont know how to create a button(only one) for mute/unmute the sound. I would like to have a mute button that can mute/unmute depending on the situation(if the music is muted then the button should have the image of a Loudspeaker,for hearing the sound, if you press it then you begin listening the music and the image of the button changes and vice versa)
what I have now is this:
<td width=10% valign="top" align="right">
<img src="images/sonidoON.png" onclick="this.src='images/sonidoOFF.png'" width=30% height=7%>
</td>
I only need to know how to create that button, not the functions for the background music.
Thank you very much in advance
回答1:
HTML
<input type="checkbox" name="un-mute" id="un-mute">
<label for="un-mute" class="unmute">
<img src="http://upload.wikimedia.org/wikipedia/commons/3/3f/Mute_Icon.svg" alt="Mute_Icon.svg" title="Mute icon">
</label>
<label for="un-mute" class="mute">
<img src="http://upload.wikimedia.org/wikipedia/commons/2/21/Speaker_Icon.svg" alt="Speaker_Icon.svg" title="Unmute/speaker icon">
</label>
CSS
input#un-mute {
display: none;
}
.unmute img {
display: none;
}
input#un-mute:checked ~ .unmute img {
display: initial;
}
input#un-mute:checked ~ .mute img {
display: none;
}
JavaScript
var un_mute = document.getElementById('un-mute');
un_mute.onclick = function() {
alert('toggle player here');
};
http://jsfiddle.net/Ffccv/2/
using :checked pseudo-class selector
回答2:
You can do this To toggle them
<script>
function toggleSound(img)
{
img.src= img.src=="images/sonidoON.png" ? "images/sonidoOFF.png" : "images/sonidoON.png";
}
</script>
and onclick="toggleSound(this);"
or create img from script and use addEventListener ("click",toggleSound())
<td width=10% valign="top" align="right">
<img src="images/sonidoON.png" onclick="toggleSound(this);" width=30% height=7%>
</td>
fiddle demo : http://jsfiddle.net/K9553/
来源:https://stackoverflow.com/questions/22918220/how-to-create-a-only-mute-unmute-button-like-youtube-in-html