如何使用JavaScript或jQuery静音视频?

时间:2018-05-25 07:21:00

标签: javascript jquery html

我想使用以下广告代码,但关闭视频后我有问题将视频静音。当我关闭(另外它在20秒后自动关闭)广告时,它在后台播放视频并且仍有声音。单击SKIP AD按钮后自动关闭后,如何将视频静音?我不了解jQuery或JavaScript。您能否修改我的代码并发布解决方案?

<script>
  window.setTimeout("document.getElementById('closead').style.display='none';", 6000);
</script>
<div class="advertisement" id="closead">
  <a target="_blank" rel="nofollow" href="http://www.sitename.com">
    <video id="dbx" style="object-fit: fill;" autoplay="" width="100%" height="100%">
      <source src="https://www.w3schools.com/tags/mov_bbb.mp4" type="video/mp4">
    </video>
  </a>
  <button onfocus="this.blur();" class="closecss" style="position:absolute;bottom: 2px;right: 0px;z-index: 999;background: #32b02b;color: #fff;padding: 13px;border-radius: 4px;font-weight: bold;cursor: pointer;border: 1px solid #2b9825;box-shadow: inset 0 1px 0 0 rgba(255, 255, 255, 0.15), 0 1px 0 0 #299023, 0 2px 3px 0 rgba(0, 0, 0, 0.25);" onclick="document.getElementById('closead').style.display='none';">SKIP AD</button>
</div>

<style>
  .advertisement {
    position: absolute;
    z-index: 99;
    height: 100%;
  }
</style>

4 个答案:

答案 0 :(得分:1)

您可以使用静音属性

静音视频
jQuery("#dbx").prop('muted', true);

答案 1 :(得分:1)

如果你可以修改上面的脚本,这是一个好方法。

&#13;
&#13;
<script>
	function removeVIdeo(videoId){
  	var video = document.querySelector(videoId);
    video.muted = true;
  }
  
  window.setTimeout(function(){
  	document.getElementById('closead').style.display='none';
    removeVIdeo('#dbx');
  },6000);
</script>
<div class="advertisement" id="closead">
  <a target="_blank" rel="nofollow" href="http://www.sitename.com">
    <video id="dbx" style="object-fit: fill;" autoplay="" width="100%" height="100%">
      <source src="https://www.w3schools.com/tags/mov_bbb.mp4" type="video/mp4">
    </video>
  </a>
  <button onfocus="this.blur();" class="closecss" style="position:absolute;bottom: 2px;right: 0px;z-index: 999;background: #32b02b;color: #fff;padding: 13px;border-radius: 4px;font-weight: bold;cursor: pointer;border: 1px solid #2b9825;box-shadow: inset 0 1px 0 0 rgba(255, 255, 255, 0.15), 0 1px 0 0 #299023, 0 2px 3px 0 rgba(0, 0, 0, 0.25);" onclick="removeVIdeo('#dbx');">SKIP AD</button>
</div>

<style>
  .advertisement {
    position: absolute;
    z-index: 99;
    height: 100%;
  }
</style>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

试试这个Yearmaz

正如Enavar的回答所说的那样

  

$(“#dbx”)。prop('muted',true);

行$(文件).ready(function(){});

.advertisement {
    position: absolute;
    z-index: 99;
    height: 100%;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
  window.setTimeout("document.getElementById('closead').style.display='none';", 6000);
</script>
<div class="advertisement" id="closead">
  <a target="_blank" rel="nofollow" href="http://www.sitename.com">
    <video id="dbx" style="object-fit: fill;" autoplay="" width="100%" height="100%">
      <source src="w3schools.com/tags/mov_bbb.mp4" type="video/mp4">
    </video>
  </a>
  <button onfocus="this.blur();" class="closecss" style="position:absolute;bottom: 2px;right: 0px;z-index: 999;background: #32b02b;color: #fff;padding: 13px;border-radius: 4px;font-weight: bold;cursor: pointer;border: 1px solid #2b9825;box-shadow: inset 0 1px 0 0 rgba(255, 255, 255, 0.15), 0 1px 0 0 #299023, 0 2px 3px 0 rgba(0, 0, 0, 0.25);" onclick="document.getElementById('closead').style.display='none';MuteVideo()">SKIP AD</button>
</div>

<script>
  function MuteVideo(){
    $("#dbx").prop('muted', true);
  }
</script>

答案 3 :(得分:0)

它将在静音和取消静音之间切换

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
$("video").click(function () {
    $(this).prop("muted", !$(this).prop("muted"));
});