我希望我的元素保持其縱橫比,但充當'全面流血'或更完整的瀏覽器體驗。視頻應該向上或向下以適應新的瀏覽器窗口尺寸,但也應該集中在瀏覽器窗口中。有時,一些視頻會被裁剪 - 這與我無關。我試圖重新創建使用純JS的視頻元素的背景封面CSS屬性,如果這有助於你想象它。對於那些誰不知道那是什麼,繼承人如下:完全流血的視頻,保持寬高比,中心,純淨javascript
.bkgdCover{
background: no-repeat center center;
-webkit-background-size: 100% 100%;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
以下是我嘗試做相同的純JavaScript:
function getWinSize(){
var wid = 0, hei = 0;
if (document.documentElement && document.documentElement.clientHeight){
wid = parseInt(window.innerWidth,10);
hei = parseInt(window.innerHeight,10);
} else if (document.body){
wid = parseInt(document.body.offsetWidth,10);
hei = parseInt(document.body.offsetHeight,10);
}
return [wid,hei];
}
this.resize2 = function(){
fullBleedVideoPlayer();
};
function fullBleedVideoPlayer() {
var viewport=getWinSize();
var videoRatio = 1080/1920;
var browserHeight = viewport[0];
var browserWidth = viewport[1];
var tmp_windowRatio = viewport[1]/viewport[0];
if (videoRatio > tmp_windowRatio) {
tmp_height = Math.round(browserWidth * videoRatio);
_player.style.width= browserWidth+ "px";
_player.style.height= tmp_height+ "px";
_player.style.marginLeft = 0 + 'px';
_player.style.marginTop = -Math.round((tmp_height - browserHeight)/2) + 'px';
} else {
tmp_width = Math.round(browserHeight * (1/videoRatio));
_player.style.width= tmp_width+ "px";
_player.style.height= browserHeight+ "px";
_player.style.marginLeft = -Math.round((tmp_width - browserWidth)/2) + 'px';
_player.style.marginTop = 0 + 'px';
}
}
不幸的是,這不會重現CSS蓋。如果你能看到我犯了什麼錯誤或自己做了同樣的事情,我很樂意聽到你的消息。請僅使用純JS解決方案。
順便說一句,我知道有一些CSS,但我試圖做的點是在哪裏定位元素和檢查什麼。雖然可以很容易地與JS做到這一點。 –