简体   繁体   English

忽略移动设备上的HTML5

[英]Ignore HTML5 on mobile devices

Right now, I have a background video that I'd like to display only on devices larger than 768px (width). 现在,我有一个背景视频,我只想在大于768像素(宽度)的设备上显示。 When you collapse the browser past 768px, the video disappears, and the poster.jpg for the video is displayed as the background instead. 当您将浏览器合拢超过768像素时,视频就会消失,而视频的poster.jpg会显示为背景。

All is working well with simple CSS, but the video is still loading on mobile devices, even though it's not being displayed. 一切都可以通过简单的CSS很好地工作,但是即使未显示视频,该视频仍可以在移动设备上加载。

Here's the HTML I'm using: 这是我使用的HTML:

<div id="video_container">
    <video id="video-bg" autoplay loop muted data-wow-duration="2s">
        <source src="/video/bg.webm" type="video/webm">
        <source src="/video/bg.mp4" type="video/mp4">
        <source src="/video/bg.ogg" type="video/ogg">
    </video>
</div>

And the SCSS: 和SCSS:

#video_container{
    background-size: cover;
    position:relative;
    z-index:-1;
    background: #000 (/video/poster.jpg) no-repeat;
    width:100%;
    height:100%;
    display:block;
    background-position:center center;
    overflow:hidden;

    #video-bg {
        position: relative;
        right: 0;
        bottom: 0;
        min-width: 100%;
        min-height: 100%;
        z-index: -100;
        display:none;
            @media(min-width: 768px){ 
                display:block;
            }
        }
}

Any help would be greatly appreciated. 任何帮助将不胜感激。

display: none will only prevent the video from being displayed, it will be loaded nevertheless. display: none只会阻止视频的显示,但仍会加载。 Use element inspector in your browser to be sure this happens. 在浏览器中使用元素检查器,以确保发生这种情况。 It seems the best way is use JavaScript. 似乎最好的方法是使用JavaScript。

You need to use JavaScript to play and pause the video whenever the screen resizes. 每当屏幕调整大小时,您都需要使用JavaScript来播放和暂停视频。

// Create a function that gets the width of the screen
// and plays or pauses the video depending its value
var handleVideo = function () {
    var width = Math.max(document.documentElement.clientWidth, window.innerWidth || 0)
    if(width > 768) {
        document.getElementById('video-bg').play();
    }
    else {
        document.getElementById('video-bg').pause();
    }
};

// Bind this function to the resize event
// and also call it to execute on first load
window.addEventListener('resize', handleVideo, false);
handleVideo();

Edit : With this approach you don't need to have the autoplay attribute (it will start playing via JS) and avoid loading the file entirely in small devices. 编辑 :使用这种方法,您不需要具有autoplay属性(它将通过JS开始播放),并且避免完全在小型设备中加载文件。

Just some jquery will help you, check this fiddle. 只是一些jQuery会帮助您,请检查此小提琴。

It will check with matchMedia that the screen is bigger than 768px ,and then, if it´s bigger, will give the url for the videos. 它将使用matchMedia检查屏幕是否大于matchMedia ,如果屏幕大于matchMedia ,则将提供视频的网址。 Then they´ll be loaded. 然后将它们加载。

 var mq = window.matchMedia("(min-width: 768px)"); if (mq.matches) { $("#source1").attr("src", "http://techslides.com/demos/sample-videos/small.ogv"); $("#source2").attr("src", "http://techslides.com/demos/sample-videos/small.ogv"); $("#source3").attr("src", "http://techslides.com/demos/sample-videos/small.ogv"); } else { alert("LITTLE SCREEN"); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="video_container"> <video id="video-bg" autoplay loop muted class="wow fadeIn" data-wow-duration="2s"> <source id="source1" src="" type="video/webm"> <source id="source2" src="" type="video/mp4"> <source id="source3" src="" type="video/ogg"> </video> </div> 

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

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