Chrome不尊重视频源内联媒体查询

时间:2014-09-18 08:38:46

标签: html5 google-chrome video html5-video media-queries

使用以下内容,Chrome不会尊重媒体查询,以根据设备宽度显示正确的视频源。 Chrome正在播放它找到的第一个来源,您可以在此处看到:http://homeglobal.ch/。我该如何解决这个问题?

    <video id="intro-video" poster="img/poster.png" controls>
        <source src="videos/intro.mp4" type="video/mp4" media="all and (min-device-width:1600px)">
        <source src="videos/intro.webm" type="video/webm" media="all and (min-device-width:1600px)">
        <source src="videos/intro-1600.mp4" type="video/mp4" media="all and (min-device-width:1100px)">
        <source src="videos/intro-1600.webm" type="video/webm" media="all and (min-device-width:1100px)">
        <source src="videos/intro-1100.mp4" type="video/mp4" media="all and (min-device-width:481px)">
        <source src="videos/intro-1100.webm" type="video/webm" media="all and (min-device-width:481px)">
        <source src="videos/intro-480.mp4" type="video/mp4">
        <source src="videos/intro-480.webm" type="video/webm">
    </video>

4 个答案:

答案 0 :(得分:8)

不幸的是,Chrome不支持视频html 5标签的媒体查询。解决这个问题的方法是使用普通的Javascript或Jquery。它不漂亮,但即使在chrome中也能正常工作。

var video = $('#myvideo');

var WindowWidth = $(window).width();

if (WindowWidth < 1200) {
    //It is a small screen
    video.append("<source src='/img/homepage/640/splash.m4v' type='video/mp4' >");
} else {
    //It is a big screen or desktop
    video.append("<source src='/img/homepage/1080/uimain-1080.mp4' type='video/mp4' >");
}

答案 1 :(得分:2)

<video class="desktop-video" autoplay playsinline preload="auto" muted loop width="100%">
    <source src="${pdict.desktop_video}" type="video/mp4">
    <source src="${pdict.desktop_video}" type="video/mov">
    Sorry, your browser doesn't support embedded videos.
</video>
<video class="mobile-video" autoplay playsinline preload="auto" muted loop width="100%">
    <source src="${pdict.mobile_video}" type="video/mp4">
    <source src="${pdict.mobile_video}" type="video/mov">
    Sorry, your browser doesn't support embedded videos.
</video>

可以在CSS中完成

.desktop-video {
    display: grid;

    @include media-breakpoint-down(lg) {
        display: none;
    }
}

.mobile-video {
    display: none;

    @include media-breakpoint-down(lg) {
        display: grid;
    }
}

答案 2 :(得分:1)

我在 Vanilla JS 上的实现。

video 标签中,作为 data-src 属性,指定默认视频的路径。在 video 标签内,在 source 标签中,我们指定两个属性 data-srcdata-mw 的值。

data-src - 是视频文件的路径。

data-mw - 是该视频应显示的最大屏幕宽度。

当屏幕宽度发生变化时,此解决方案会自动生效。这是使用 resize() 函数完成的。

<video id="intro-video" data-src="/path/default.mp4" poster="img/poster.png" controls>
    <source data-src="/path/1600.mp4" data-mw="1600">
    <source data-src="/path/900.mp4" data-mw="900">
    <source data-src="/path/480.mp4" data-mw="480">
</video>
class VideoResponser {
    constructor(selector) {
        const $video = document.querySelector(selector);
        this.options = { 
            selector, 
            breakpoints: { default: { src: $video.getAttribute('data-src') } } 
        };

        // get a list of video switching points and links to the videos themselves 
        $video.querySelectorAll('[data-src]').forEach(element => this.options.breakpoints[element.getAttribute('data-mw')] = { src: element.getAttribute('data-src') });
        $video.innerHTML = ''; // we clean up so that there is nothing superfluous 
        
        // run the handler and track the change in screen width
        this.responseVideo(this.options);
        this.resizer();
    }

    /** Function runs on resize  */
    resizer() {
        window.addEventListener("resize", () => this.responseVideo(this.options));
    }

    /** 
     * Change src value of video link to fit screen width 
     * 
     * @param {Object} options object with options 
     */
    responseVideo(options) {
        const {selector, breakpoints} = options; // get options
        let $video = document.querySelector(selector);
        const widthNow = $video.getAttribute('data-width-now') || null;
        const maxBreakpoint = Math.max(Object.keys(breakpoints).filter(key => key >= document.body.clientWidth));
        const nowBreakpoint = maxBreakpoint || 'default'; // choose either the maximum value, if not, then the default 

        if(widthNow && widthNow == nowBreakpoint) return; // check if the video needs to be changed 

        $video.setAttribute('data-width-now', nowBreakpoint);
        $video.src = breakpoints[nowBreakpoint].src;
    }
}

new VideoResponser("#intro-video");

答案 3 :(得分:-1)

我在object-fit: cover;上使用了解决方案

.video-wrapper {
    position: relative;
    max-width: 100%;
    height: auto;
    overflow: hidden;
    margin-bottom: 0;
    padding-top: 100%;
}

@media (min-width: 768px) {
        .video-wrapper {
                padding-top: 50%;
        }
    }

.video-item {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    object-fit: cover;
}

Codepen here