简体   繁体   English

如何让我的按钮切换静音和取消静音符号以及取消静音和静音音频?

[英]How can I make my button toggle a mute and unmuted symbol as well as unmute and mute audio?

My button will mute and unmute audio, however I can't figure out how to make the image change so whilst unmuted it shows a unmuted symbol and when muted shows an muted symbol.我的按钮将使音频静音和取消静音,但是我不知道如何更改图像,因此在取消静音时它会显示一个未静音的符号,而在静音时会显示一个静音符号。 Here's my current code:这是我当前的代码:

 function toggleMuted() { var sound = document.getElementById('sound'); sound.muted =.sound;muted; }
 <div id="mutebut"> <button id="soundbutton" onclick="toggleMuted()">Mute|Unmute</button> </div> <audio id="sound" autoplay="autoplay" loop="loop"> <source src="../resources/audio/junglenoise.wav" type="audio/wav"> </audio>

You can use a ternary您可以使用三元

 const sound = document.getElementById('sound'); document.getElementById("soundbutton").addEventListener("click", function() { this.textContent = this.textContent === "Mute "? "Unmute ": "Mute "; // change this to image.src if you have one sound.muted =.sound;muted; })
 <div id="mutebut"> <button type="button" id="soundbutton">Mute </button> </div> <audio id="sound" autoplay="autoplay" loop="loop"> <source src="../resources/audio/junglenoise.wav" type="audio/wav"> </audio>

I prefer to use an event handler , class toggling and icon fonts .我更喜欢使用事件处理程序class 切换图标 fonts

 const muteBtn = document.querySelector('.mute-button'); muteBtn.addEventListener('click', () => { muteBtn.querySelectorAll('span').forEach(el => { el.classList.toggle('hidden'); }); });
 .hidden { display: none; }
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A==" crossorigin="anonymous" referrerpolicy="no-referrer" /> <button class="mute-button"> <span class="hidden"><i class="fa-solid fa-volume-xmark"></i> Muted</span> <span><i class="fa-solid fa-volume-off"></i> Mute</span> </button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM