获取任意Html元素与body之间的偏移距离 offsetTop、offsetLeft

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312">
  3. <head>
  4. <head>
  5. <title> 代码实例:获取任意Html元素与body之间的偏移距离 offsetTop、offsetLeft </title>
  6. <meta http-equiv="content-type" content="text/html; charset=gb2312" />
  7. <meta content="枫岩,CNLei.y.l@gmail.com">
  8. <style type="text/css" media="all">
  9. body,p {margin:0;padding:0;font-size:12px;}
  10. body {float:left;width:100%;}
  11. ul,ul li {margin:0;padding:0;list-style:none;padding:0;}
  12. ul li input {border:1px solid #ccc;}
  13. #Bd {
  14. background:#FFE8D9;
  15. float:left;
  16. padding:20px;
  17. border:10px solid #f90;/*该值在IE下还是取不到*/
  18. width:100%;
  19. }
  20. #BS {
  21. padding:20px;
  22. float:left;
  23. background:#58CB64;
  24. }
  25. #BS ul {border:20px solid #DDF1D8;}
  26. #BM {
  27. margin-top:100px;
  28. float:right;
  29. width:300px;
  30. background:#fff;
  31. }
  32. </style>
  33. <script type="text/javascript">
  34. var w3c=(document.getElementById)? true:false;
  35. var agt=navigator.userAgent.toLowerCase();
  36. var ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1) && (agt.indexOf("omniweb") == -1));
  37. var ie5=(w3c && ie)? true : false;
  38. var ns6=(w3c && (navigator.appName=="Netscape"))? true: false;
  39. var op8=(navigator.userAgent.toLowerCase().indexOf("opera")==-1)? false:true;
  40. function Obj(o){
  41.  return document.getElementById(o)?document.getElementById(o):o;
  42. }
  43. function GetXYWH(o){
  44. var nLt=0;
  45. var nTp=0;
  46.  var ooffsetParent = o;
  47.  while (offsetParent!=null && offsetParent!=document.body) {
  48.  nLt+=offsetParent.offsetLeft;
  49.  nTp+=offsetParent.offsetTop;
  50.  if(!ns6){
  51.  parseInt(offsetParent.currentStyle.borderLeftWidth)>0?nLt+=parseInt(offsetParent.currentStyle.borderLeftWidth):"";
  52.  parseInt(offsetParent.currentStyle.borderTopWidth)>0?nTp+=parseInt(offsetParent.currentStyle.borderTopWidth):"";
  53.  }
  54.  offsetParentoffsetParent=offsetParent.offsetParent;
  55.  //alert(offsetParent.tagName);
  56.  }
  57. alert("ID:"+o.id+"/n/nL:"+nLt+" T:"+nTp+"/nW:"+o.offsetWidth+" H:"+o.offsetHeight);
  58. }
  59. </script>
  60. </head>
  61. <body>
  62. <p style="height:100px;margin:0;padding:0;background:#00f;color:#fff;line-height: 100px;text-align:center;">此色块高:100px;</p>
  63. <div >
  64. <div >
  65. <ul>
  66. <li><input type="text" value="无法取到橙黄色的边框线宽度(border:10px solid #f90;)" size="60" /></li>
  67. <li><input type="text" value="" size="60" onclick="GetXYWH(this)" /></li>
  68. <li><input type="text" value="GetXYWH(this);" onclick="GetXYWH(this)" size="60" /></li>
  69. <li><input type="text" value="GetXYWH(this);" onclick="GetXYWH(this)"size="60" /></li>
  70. <li><input type="text" value="GetXYWH(Obj('BM'));'BM'));" size="60" onclick="GetXYWH(Obj('BM'));' BM'))" /></li>
  71. </ul>
  72. </div><!--BS-->
  73. <div>
  74. <p>测试</p>
  75. <p>测试</p>
  76. <p>测试</p>
  77. <p>测试</p>
  78. <p>测试</p>
  79. </div><!--BM-->
  80. </div>
  81. </body>
  82. </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值