html - 应用媒体查询时视频静音

标签 html css audio video

谁能告诉我如何让视频在特定的屏幕尺寸宽度下静音:

<div class="fullscreen-bg">
    <video loop autoplay poster="img/videoframe.jpg" class="fullscreen-bg__video">
        <source src="vid/Marbella%20Triathlon%20Start-HD.mp4" type="video/mp4">
    </video>
</div>


@media (max-width: 767px) {
    .fullscreen-bg {
        background: url('../img/brownlee.jpg') center center / cover no-repeat;
        background-position: 40% 0%;
    }

    .fullscreen-bg__video {
        display: none;
    }
}

我在下面找到这段代码,对它做了一些小改动,但我对编码还很陌生,不知道如何应用它,以及它是否正确:

  <script>
  $(fucntion() {
    if(document.body.clientWidth >= 767) {
        $('video').attr('muted', false);
    }

    $(window).resize(function() {
        if(document.body.clientWidth <= 767) {
            $('video').attr('muted', true);
        }
    });
});
</script>

最佳答案

<script>
    (function() {
        'use strict';
        var videos = document.querySelectorAll('video.mute-on-resize');//get all video tags with class="mute-on-resize". You can replace it any other CSS selector.
        var length = videos.length;//get total number of tags
        if (length === 0) {//check if total is 0
            return;
        }
        function toggleMute(e) {//function to change mute of all videos
            for (var i = 0; i < length; i++) {
                videos[i].muted = e;
            }
        };
        function resizeHandle() {//function to check size
            toggleMute(innerWidth >= 767);
        };
        addEventListener('resize', resizeHandle);//run function when window resizes
        resizeHandle();//run function once on load
    })();
</script>

在 body 标签的末尾添加此脚本标签。将“调整大小静音”类添加到您想要静音的任何视频,尺寸为 767 或更大。

关于html - 应用媒体查询时视频静音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35971906/

相关文章:

html - 为什么我的图像标题的高度会根据下面的元素发生变化?

audio - ADPCM是否有一定的采样率?

php - 动态下拉框?

javascript - 将模态表单重定向到我的 php 脚本

java - java中如何获取字符串变量的值

CSS:在悬停时减轻元素

javascript - 使用多选更改样式

html - 对于 IE 7,表格的 <tr> 似乎不能有边框,但我也没有在谈论 <tr> 边框的书中看到?

html - 如何在使用 m4a 声音文件时使用 CSS 在我的网站上嵌入音乐?而且还有控制权?

java - 如何在android中合并两个音频文件