這是很老了,但人們可能仍然需要在這裏幫助。我需要這也因此創造了我的解決方案的筆,這應有助於 - 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"});
});
好的,但如果屏幕太高怎麼辦?據我所知,如果視頻嵌入到div中,div將根據屏幕大小進行調整和調整大小。但是,在這種情況下,身體是容器,我希望視頻完全填充它。我不能控制它的大小,因爲它取決於屏幕/瀏覽器的大小... – Owly 2014-10-28 21:21:09