这已经很老了,但人们可能仍然需要帮助。我也需要这个,所以我创建了一个我的解决方案的笔,这应该会有所帮助 - http://codepen.io/MikeMooreDev/pen/QEEPMv
该示例显示了同一视频的两个版本,一个为标准版本,第二个被裁剪并居中对齐以适应整个父元素的大小,而不显示可怕的黑条。
您需要使用一些 javascript 来计算视频的新宽度,但如果您知道宽高比,这真的很容易。
HTML
<div id="videoWithNoJs" class="videoWrapper">
<iframe src="https://www.youtube.com/embed/ja8pA2B0RR4" frameborder="0" allowfullscreen></iframe>
</div>
CSS - videoWrapper 的高度和宽度可以是任何值,如果你愿意,它们可以是百分比
.videoWrapper {
height:600px;
width:600px;
position:relative;
overflow:hidden;
}
.videoWrapper iframe {
height:100%;
width:100%;
position:absolute;
top:0;
bottom:0;
}
jQuery
$(document).ready(function(){
// - 1.78 is the aspect ratio of the video
// - This will work if your video is 1920 x 1080
// - To find this value divide the video's native width by the height eg 1920/1080 = 1.78
var aspectRatio = 1.78;
var video = $('#videoWithJs iframe');
var videoHeight = video.outerHeight();
var newWidth = videoHeight*aspectRatio;
var halfNewWidth = newWidth/2;
video.css({"width":newWidth+"px","left":"50%","margin-left":"-"+halfNewWidth+"px"});
});
这也可以在调整大小时触发,以确保它保持响应。最简单(可能不是最有效)的方法是使用以下方法。
$(window).on('resize', function() {
// Same code as on load
var aspectRatio = 1.78;
var video = $('#videoWithJs iframe');
var videoHeight = video.outerHeight();
var newWidth = videoHeight*aspectRatio;
var halfNewWidth = newWidth/2;
video.css({"width":newWidth+"px","left":"50%","margin-left":"-"+halfNewWidth+"px"});
});