var docWidth = document.body.offsetWidth;
// var docHeight = document.body.offsetHeight;
var divobj = document.getElementById("align");
var divWidth = divobj.offsetWidth;
// var divHeight = divobj.offsetHeight;
//让图片左右居中显示
var marginleft=(docWidth-divWidth)/2;
divobj.style.marginLeft = marginleft +"px";
//让图片上下居中显示
//var margintop=(docHeight-divHeight)/2;
//divobj.style.marginTop = margintop + "px";
转载于:https://www.cnblogs.com/wuchao/archive/2012/08/01/2618408.html