如何模拟background-size:coverhtml 元素上的功能,例如<video>或<img>?
我希望它像
background-size: cover;
background-position: center center;
如何模拟background-size:coverhtml 元素上的功能,例如<video>或<img>?
我希望它像
background-size: cover;
background-position: center center;
这是我拉了一段时间的东西,但我遇到了一个很好的解决方案,它不使用任何脚本,并且可以使用 5 行 CSS(如果算上选择器和括号,则为 9 行)在视频上实现完美的封面模拟)。这有 0 个边缘情况,它不能完美地工作,缺少 CSS3-compatibility。
你可以在这里看到一个例子(存档)
Timothy 的解决方案的问题在于它不能正确处理缩放。如果周围的元素小于视频文件,则不会按比例缩小。即使你给视频标签一个很小的初始大小,比如 16 像素 x 9 像素,auto最终会迫使它最小化其原生文件大小。使用此页面上当前最高投票的解决方案,我不可能缩小视频文件的比例,从而产生剧烈的缩放效果。
但是,如果您的视频的纵横比已知,例如 16:9,您可以执行以下操作:
.parent-element-to-video {
    overflow: hidden;
}
video {
    height: 100%;
    width: 177.77777778vh; /* 100 * 16 / 9 */
    min-width: 100%;
    min-height: 56.25vw; /* 100 * 9 / 16 */
}
如果视频的父元素设置为覆盖整个页面(例如position: fixed; width: 100%; height: 100vh;),则视频也将覆盖整个页面。
如果您还希望视频居中,则可以使用surefire 居中方法:
/* merge with above css */
.parent-element-to-video {
    position: relative; /* or absolute or fixed */
}
video {
    position: absolute;
    left: 50%; /* % of surrounding element */
    top: 50%;
    transform: translate(-50%, -50%); /* % of current element */
}
当然,vw,vh和transform是 CSS3,所以如果您需要与更旧的浏览器兼容,您将需要使用脚本。
使用背景覆盖对图像来说很好,宽度 100% 也是如此。这些不是最佳的<video>,而且这些答案过于复杂。您不需要 jQuery 或 JavaScript 来完成全宽视频背景。
请记住,我的代码不会像封面那样用视频完全覆盖背景,而是会使视频尽可能大以保持纵横比并仍然覆盖整个背景。任何多余的视频都会从页面边缘溢出,这取决于您锚定视频的位置。
答案很简单。
只需使用此 HTML5 视频代码或类似以下内容的内容:(在整页中测试)
html, body {
  width: 100%; 
  height:100%; 
  overflow:hidden;
}
#vid{
  position: absolute;
  top: 50%; 
  left: 50%;
  -webkit-transform: translateX(-50%) translateY(-50%);
  transform: translateX(-50%) translateY(-50%);
  min-width: 100%; 
  min-height: 100%; 
  width: auto; 
  height: auto;
  z-index: -1000; 
  overflow: hidden;
}
<video id="vid" video autobuffer autoplay>
  <source id="mp4" src="http://grochtdreis.de/fuer-jsfiddle/video/sintel_trailer-480.mp4" type="video/mp4">
</video>
min-height 和 min-width 将允许视频保持视频的纵横比,这通常是任何普通浏览器在正常分辨率下的纵横比。任何多余的视频都会从页面的一侧流出。
这是我如何做到的。一个工作示例在这个 jsFiddle 中。
var min_w = 300; // minimum video width allowed
var vid_w_orig;  // original video dimensions
var vid_h_orig;
jQuery(function() { // runs after DOM has loaded
  vid_w_orig = parseInt(jQuery('video').attr('width'));
  vid_h_orig = parseInt(jQuery('video').attr('height'));
  $('#debug').append("<p>DOM loaded</p>");
  jQuery(window).resize(function () { resizeToCover(); });
  jQuery(window).trigger('resize');
});
function resizeToCover() {
  // set the video viewport to the window size
  jQuery('#video-viewport').width(jQuery(window).width());
  jQuery('#video-viewport').height(jQuery(window).height());
  // use largest scale factor of horizontal/vertical
  var scale_h = jQuery(window).width() / vid_w_orig;
  var scale_v = jQuery(window).height() / vid_h_orig;
  var scale = scale_h > scale_v ? scale_h : scale_v;
  // don't allow scaled width < minimum video width
  if (scale * vid_w_orig < min_w) {scale = min_w / vid_w_orig;};
  // now scale the video
  jQuery('video').width(scale * vid_w_orig);
  jQuery('video').height(scale * vid_h_orig);
  // and center it by scrolling the video viewport
  jQuery('#video-viewport').scrollLeft((jQuery('video').width() - jQuery(window).width()) / 2);
  jQuery('#video-viewport').scrollTop((jQuery('video').height() - jQuery(window).height()) / 2);
  // debug output
  jQuery('#debug').html("<p>win_w: " + jQuery(window).width() + "</p>");
  jQuery('#debug').append("<p>win_h: " + jQuery(window).height() + "</p>");
  jQuery('#debug').append("<p>viewport_w: " + jQuery('#video-viewport').width() + "</p>");
  jQuery('#debug').append("<p>viewport_h: " + jQuery('#video-viewport').height() + "</p>");
  jQuery('#debug').append("<p>video_w: " + jQuery('video').width() + "</p>");
  jQuery('#debug').append("<p>video_h: " + jQuery('video').height() + "</p>");
  jQuery('#debug').append("<p>vid_w_orig: " + vid_w_orig + "</p>");
  jQuery('#debug').append("<p>vid_h_orig: " + vid_h_orig + "</p>");
  jQuery('#debug').append("<p>scale: " + scale + "</p>");
};
#video-viewport {
  position: absolute;
  top: 0;
  overflow: hidden;
  z-index: -1; /* for accessing the video by click */
}
#debug {
  position: absolute;
  top: 0;
  z-index: 100;
  color: #fff;
  font-size: 12pt;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="video-viewport">
  <video autoplay controls preload width="640" height="360">
    <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"type="video/mp4" />
    <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.webm"type="video/webm" />
    <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.ogv"type="video/webm" />
  </video>
</div>
<div id="debug"></div>
根据Daniel de Wit 的回答和评论,我进行了更多搜索。感谢他的解决方案。
解决方案是使用object-fit: cover;它有很大的支持(每个现代浏览器都支持它)。如果你真的想支持 IE,你可以使用像object-fit-images或object-fit这样的 polyfill 。
演示:
img {
  float: left;
  width: 100px;
  height: 80px;
  border: 1px solid black;
  margin-right: 1em;
}
.fill {
  object-fit: fill;
}
.contain {
  object-fit: contain;
}
.cover {
  object-fit: cover;
}
.none {
  object-fit: none;
}
.scale-down {
  object-fit: scale-down;
}
<img class="fill" src="http://www.peppercarrot.com/data/wiki/medias/img/chara_carrot.jpg"/>
<img class="contain" src="http://www.peppercarrot.com/data/wiki/medias/img/chara_carrot.jpg"/>
<img class="cover" src="http://www.peppercarrot.com/data/wiki/medias/img/chara_carrot.jpg"/>
<img class="none" src="http://www.peppercarrot.com/data/wiki/medias/img/chara_carrot.jpg"/>
<img class="scale-down" src="http://www.peppercarrot.com/data/wiki/medias/img/chara_carrot.jpg"/>
并与父母:
div {
  float: left;
  width: 100px;
  height: 80px;
  border: 1px solid black;
  margin-right: 1em;
}
img {
  width: 100%;
  height: 100%;
}
.fill {
  object-fit: fill;
}
.contain {
  object-fit: contain;
}
.cover {
  object-fit: cover;
}
.none {
  object-fit: none;
}
.scale-down {
  object-fit: scale-down;
}
<div>
<img class="fill" src="http://www.peppercarrot.com/data/wiki/medias/img/chara_carrot.jpg"/>
</div><div>
<img class="contain" src="http://www.peppercarrot.com/data/wiki/medias/img/chara_carrot.jpg"/>
</div><div>
<img class="cover" src="http://www.peppercarrot.com/data/wiki/medias/img/chara_carrot.jpg"/>
</div><div>
<img class="none" src="http://www.peppercarrot.com/data/wiki/medias/img/chara_carrot.jpg"/>
</div><div>
<img class="scale-down" src="http://www.peppercarrot.com/data/wiki/medias/img/chara_carrot.jpg"/>
</div>