设置外部样式坐标的位置
<style type="text/css"> #main{text-align:center; background-color:#9FF; height:600px;width:800px} #adver{ position:absolute; left:50px; top:30px; z-index:2; } </style> <script src="js/scroll.js"></script> </head> <body> <div id="adver"><img src="images/adv.jpg"/></div> <div id="testDiv" style="position:absolute;left:300px;top:50px;background-color:red; width:80px; height:200px">内部样式的div</div> <input type="button" value="测试外部样式坐标位置" onClick="test('adver')"/> <div id="main">网页内容</div> </body>
function test (divID){ var divObj=document.getElementById(divID); var leftValue,leftValue; //alert( .appName); //使用navigator判断有时不准确,例如:在FF下运行判断为Netscape if(divObj.currentStyle){ //IE leftValue=divObj.currentStyle.left; topValue=divObj.currentStyle.top; }else{ //FF leftValue=document.defaultView.getComputedStyle(divObj,null).left; topValue=document.defaultView.getComputedStyle(divObj,null).top; } alert("left="+leftValue+"\ntop="+topValue); divObj.style.left=parseInt(leftValue)+200+"px"; divObj.style.top=parseInt(topValue)+100+"px"; }