css - 更改我的音乐按钮打开和关闭时的图像

标签 css html

我的页面上有一个按钮可以打开和关闭音乐我已经用 css 设置了它的样式以显示自定义图像...我可以在打开和关闭时更改此按钮的图像吗 谢谢

这是我的音频代码

<audio id="myAudio"
                    <source src="http://media.w3.org/2010/07/bunny/04-Death_Becomes_Fur.mp4"
         type='audio/mp4'>
                    <source src="http://media.w3.org/2010/07/bunny/04-Death_Becomes_Fur.oga"
         type='audio/ogg; codecs=vorbis'>
                    Your user agent does not support the HTML5 Audio element.
                    </audio>
                    <script>
function aud_play_pause() {
  var myAudio = document.getElementById("myAudio");
  if (myAudio.paused) {
    myAudio.play();
  } else {
    myAudio.pause();
  }
}
</script>

这是我的html按钮

<button class="soundButton" type="button" onclick="aud_play_pause()"></button>

这是我的CSS样式

.soundButton{
    width:23px;
    height:18px;
    border:none;
    background:url(../../images/speakerIcon.png);
    padding:0;
    margin:0;
}

最佳答案

你能试试这个吗,

function aud_play_pause(d) {
  var myAudio = document.getElementById("myAudio");
  if (myAudio.paused) {
     d.className = 'soundButton';
    myAudio.play();
  } else {
     d.className = 'soundOffButton';
    myAudio.pause();
  }
}

HTML:

<button class="soundButton" type="button" onclick="aud_play_pause(this)"></button>

CSS:

.soundOffButton{
    width:23px;
    height:18px;
    border:none;
    background:url(../../images/speakerIconOFF.png);
    padding:0;
    margin:0;
}

关于css - 更改我的音乐按钮打开和关闭时的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20818187/

相关文章:

html - 为什么 'lightblue' 不是有效的 CSS 颜色?

css - @font-face 多个字体系列(名称别名)

javascript - 构建静态投资组合的最快方法?

html - 两个元素之间的空间

html - 向 Logo 图像添加文本的最佳方式

html - 如果其子元素具有绝对位置,如何获取父元素的高度

html - 导航栏突然在顶部而不是底部 HTML

javascript - 如何在 Jquery 中使用复选框选择元素时删除该元素?

javascript - 如何通过一个按钮用数组中的下一个值填充表格

javascript - 在 asp.net c# 中使用 jquery 进行 ckeditor 验证