日期:2014-05-16 浏览次数:20316 次
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <style> #align{ width: 600px; height: 300px; border: 1px solid black; } .center{ margin: 0 auto; } </style> <script> var adjustDivPosition = function() { var docWidth = document.body.offsetWidth; var docHeight= ( 'innerHeight' in window? window.innerHeight : document.compatMode!=='BackCompat'? document.documentElement.clientHeight : document.body.clientHeight ); 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"; }; </script> </head> <body> <div id="align"></div> <script> adjustDivPosition(); </script> </body> </html>