日期:2014-05-16 浏览次数:20327 次
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>pos center </title> <style type="text/css" > body{ position:relative;} #div1{ position:absolute; border:1px solid red; } </style> </head> <body> <div id="div1">some text hello world<br /> some text </div> <input type="button" value="DIV定位正中" style="position:absolute;left:300px;" onclick="getwidthheight()" /> <script type="text/javascript"> var div1 = document.getElementById("div1"); function getwidthheight() { var windowwidth = document.documentElement.clientWidth; var windowheight = document.documentElement.clientHeight; var divwidth = div1.clientWidth; var divheight = div1.clientHeight; div1.style.left = (windowwidth - divwidth) / 2 + "px"; div1.style.top = (windowheight - divheight) / 2 + "px"; } </script> </body> </html>