如何在<video><img>等html元素上模拟background-size:cover的功能?

我希望它能像这样工作

background-size: cover;
background-position: center center;

推荐答案

我是这样做的.一个有效的例子是在this 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>

Jquery相关问答推荐

ASP.NET MVC - Ajax 将空值传递给控制器

使用jquery将外部代码插入div

用 JS 触发 CSS 悬停

jQuery:使用变量作为 Select 器

jQuery ajax 调用默认超时值

为什么我们在 jQuery 中使用({ })?

在 `click` 和 `enter` 上触发事件

以ajax方式在rails 3中提交表单(使用jQuery)

如何使用 jQuery Select 单个子元素?

如何在 JQuery UI 自动完成中使用 source:function()... 和 AJAX

如何使用 jQuery UI Resizable 仅水平或垂直调整大小?

jQuery.extend 和 jQuery.fn.extend 的区别?

停止输入/书写后如何触发输入文本中的事件?

判断复选框是否未选中单击 - jQuery

如何使用 JSON、jQuery 将一组复杂对象发布到 ASP.NET MVC 控制器?

使用 jQuery 的 ajax 方法将图像作为 blob 检索

将多个 JavaScript 文件合并为一个 JS 文件

jQuery:如何找到父母的特定子元素?

jQuery slideUp().remove() 在删除发生之前似乎没有显示 slideUp 动画

jQuery .val 更改不会更改输入值