自适应背景图,不变形,不留空白

var backgroundImgDiv = document.getElementById("backgroundImgDiv");
var img = backgroundImgDiv.getElementsByTagName("img")[0];

(setImgSize=function(){
var windowWidth = document.documentElement.clientWidth || document.body.clientWidth;
var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
document.body.style.height = windowHeight + "px";
var cale = windowWidth/windowHeight;
if (cale >= 1920/1080) {
backgroundImgDiv.style.width = windowWidth+'px';
img.style.width = windowWidth+'px';

backgroundImgDiv.style.height = "auto";
img.style.height= "auto";
} else{
backgroundImgDiv.style.width = "auto";
img.style.width = "auto";

backgroundImgDiv.style.height = windowHeight+'px';
img.style.height= windowHeight+'px';
}
})();
window.onresize=setImgSize;

原文地址:https://www.cnblogs.com/luckyXcc/p/5946492.html