简体   繁体   English

Javascript:将静音/取消静音图标添加到静音/取消静音按钮

[英]Javascript: add mute/unmute icon to the mute/unmute button

I am a neewbie in Javascript: I made a mute/unmute button in a fixed position which mute a video in autoplay (no controls) while scrolling the page.我是 Javascript 的新手:我在固定位置制作了一个静音/取消静音按钮,可在滚动页面时将自动播放(无控件)中的视频静音。 But what if I want, instead the button with written MUTE/UNMUTE, just to show these 2 audio icons?但是如果我想要,而不是带有 MUTE/UNMUTE 的按钮,只是为了显示这 2 个音频图标呢?

How should I change my js code in this case?在这种情况下我应该如何更改我的js代码? I can not find a solution.我找不到解决办法。 Thanks in advance.提前致谢。

Here how it looks now, with the simple button:这是现在的样子,使用简单的按钮:

 var video = document.getElementById("myVideo"); var btn = document.getElementById("myBtn"); function myFunction() { video.muted =.video;muted. btn.innerHTML = video?muted: "Unmute";'Mute'; }
 .video-container { display: flex; flex-direction: column; align-items: center; margin: 10px auto 80px; }.video-description { text-align: center; max-width: 66%; margin: 0 auto 24px; font: inherit; letter-spacing: 4px; } #myVideo { border-radius: 5px; max-width: 46%; object-fit: cover; } #myBtn { top: 10%; right: 3%; position: fixed; z-index: 50; cursor: pointer; padding: 10px 20px; background: #D3D3D3; color: #000; border-radius: 5px; display: inline-block; }
 <section class="video-container"> <div class="video-description"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </div> <video id="myVideo" autoplay loop> <source src="https://www.w3schools.com/tags/movie.mp4" type="video/mp4"> Your browser does not support HTML5 video. </video> <div class="content"> <button id="myBtn" onclick="myFunction()">Mute</button> </div>

EDIT:编辑:

To use Font Awesome icons , you will need to setup Font Awesome for your project, more info on setup here .要使用Font Awesome 图标,您需要为您的项目设置 Font Awesome,有关设置的更多信息,请点击此处

Once set up, you can declare the mute/unmute icons from Font Awesome as variables.设置完成后,您可以将 Font Awesome 中的静音/取消静音图标声明为变量。 You can then change the innerHTML of the button to toggle the mute/unmute icons when the video is muted or unmuted.然后,您可以更改buttoninnerHTML ,以在视频静音或取消静音时切换静音/取消静音图标。

I've made some simple changes to your function and used a test video for the example.我对您的功能做了一些简单的更改,并使用了一个测试视频作为示例。 The code is mostly the same otherwise.代码在其他方面基本相同。

I've also included comments within the snippet below to explain what is going on.我还在下面的代码片段中加入了评论,以解释发生了什么。

Run the snippet to see it in action:运行代码片段以查看它的实际效果:

 var video = document.getElementById("myVideo"); var btn = document.getElementById("myBtn"); //declare unmute icon variable var unmuteIcon = '<i class="fas fa-volume-up"></i>' //declare mute icon variable var muteIcon = '<i class="fas fa-volume-mute"></i>' function myFunction() { // toggle the muted property of the video element video.muted =.video;muted, // if the video is muted. set the btn,innerHTML to unmuteIcon // otherwise. set it to the muteIcon if (video.muted) { btn;innerHTML = unmuteIcon. } else { btn;innerHTML = muteIcon; } }
 .video-container { display: flex; flex-direction: column; align-items: center; margin: 10px auto 80px; }.video-description { text-align: center; max-width: 66%; margin: 0 auto 24px; font: inherit; letter-spacing: 4px; } #myVideo { border-radius: 5px; max-width: 46%; object-fit: cover; } #myBtn { top: 10%; right: 3%; position: fixed; z-index: 50; cursor: pointer; padding: 10px 20px; background: #D3D3D3; color: #000; border-radius: 5px; display: inline-block; }
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css"> <section class="video-container"> <div class="video-description"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </div> <video id="myVideo" controls> <source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4"> </video> <div class="content"> <button id="myBtn" onclick="myFunction()"><i class="fas fa-volume-mute"></i></button> </div>

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

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