首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >覆盖所有可用空间的全屏背景视频

覆盖所有可用空间的全屏背景视频
EN

Stack Overflow用户
提问于 2014-03-14 17:34:53
回答 1查看 388关注 0票数 1

基本上,我希望视频的行为与background-size:cover的工作方式相同-覆盖所有可用的空间-例如:http://www.aaronvanderzwan.com/maximage/examples/html5video.html。我让它按比例调整大小并居中--但它仍然没有覆盖所有可用的空间。

Javascript:

代码语言:javascript
复制
$(document).ready(function(e){

  var $item = $(".video");
  var proportions =  $item.width() / $item.height()


  // shim layer with setTimeout fallback
  window.requestAnimFrame = (function(){
    return  window.requestAnimationFrame       ||
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame    ||
            function( callback ){
              window.setTimeout(callback, 1000 / 60);
            };
  })();



  // usage:
  // instead of setInterval(render, 16) ....
  (function animloop(){
    requestAnimFrame(animloop);
    resize();
  })();


  function resize(){

    // center the item
    $item.css({"top": "50%", "margin-top":-parseInt($item.height()/2)})
    $item.css({"left": "50%", "margin-left":-parseInt($item.width()/2)})


    // scale it
    if($(window).width() / $(window).height() < proportions){
      scaleProportionalByHeight($(window).height())
    }else{
      scaleProportionalByWidth( $(window).width() )
    }
  }



  function scaleProportionalByWidth ( newWidth ) {
    $item.width(newWidth);
    $item.height(newWidth / proportions);
  }

  function scaleProportionalByHeight ( newHeight  )  {
    $item.height(newHeight);
    $item.width(newHeight * proportions);
  }

})

html:

代码语言:javascript
复制
<video class="video"  loop autoplay muted autobuffer="autobuffer">
  <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" type="video/mp4">
</video>
EN

回答 1

Stack Overflow用户

发布于 2014-03-14 17:51:59

你不能只使用CSS吗?

如下所示:

HTML

代码语言:javascript
复制
<video loop autoplay muted autobuffer="autobuffer" id="myVideo">
    <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" type="video/mp4">
</video>

CSS

代码语言:javascript
复制
video#myVideo{
    position: fixed; right: 0; bottom: 0;
    width: auto; height: auto; z-index: -100;
    min-width: 100%; min-height: 100%;
    background-size: cover;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22401064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档