-2
我想加載一個隨機的背景圖像使用JavaScript每次加載一個網站。背景應該覆蓋整個屏幕。我的兩個背景不一樣,一個像方形圖像,另一個是更短更寬的圖像。無論選擇哪個,我都希望能夠填滿整個屏幕。 我試圖將背景覆蓋屬性應用於CSS,但它似乎失敗。無論如何,簡短的背景並不會延伸。網站背景圖片不包括
更新 現在它
html{
height: 100%;
width: 100%;
}
body{
margin : 0;
padding: 0;
min-height:100%;
font-size: 17px;
}
function loadRandomBackground() {
var backgroundNumber = Math.floor((Math.random() * 2) + 1);
document.body.style.background = "url('img/bg" + backgroundNumber + ".jpg')";
document.body.style.backgroundRepeat = "no-repeat";
document.body.style.backgroundPosition = "center";
document.body.style.backgroundSize = "cover";
}
css代碼請但我的猜測是你沒有使身體100%的視口高度 – Pete
我設法讓它去。 – sarakinos