• .offsetLeft,.offsetTop


    *{ margin:0; padding:0}
    div {padding: 40px 50px;}
    #div1 {background: red;}
    #div2 {background: green; }
    #div3 {background: orange;}
    
    <div id="div1">
      <div id="div2">
        <div id="div3"></div>
      </div>
    </div>
    
    <script> 
    window.onload = function() {
    var oDiv3 = document.getElementById('div3');
        alert( oDiv3.offsetTop );
    }
    </script>
    
    
    offsetTop 
    div3无定位父级则显示为到body的距离
    火狐,谷歌,360,opera:80
    ie7-11:80

    offsetLeft

    div3无定位父级则显示为到body的距离
    火狐,谷歌,360,opera:100
    ie7-11:100
     
    *{ margin:0; padding:0}
    div {padding: 40px 50px;}
    #div1 {background: red;}
    #div2 {background: green;position:relative }
    #div3 {background: orange;}
    
    <div id="div1">
      <div id="div2">
        <div id="div3"></div>
      </div>
    </div>
    
    <script> 
    window.onload = function() {
    var oDiv3 = document.getElementById('div3');
        alert( oDiv3.offsetTop );
    }
    </script>
    offsetTop
    div3有定位父级则显示为到div2(父级)的距离
    火狐,谷歌,360,opera:40
    ie8-11:40
    ie7:80 

    offsetLeft
    div3有定位父级则显示为到div2(父级)的距离
    火狐,谷歌,360,opera:40
    ie8-11:50
    ie7:100如果自己没有定位,那么offsetLeft[Top]是到body的距离
     
    *{ margin:0; padding:0}
    div {padding: 40px 50px;}
    #div1 {background: red;}
    #div2 {background: green;position: relative; }
    #div3 {background: orange;position: relative;}
    
    <div id="div1">
      <div id="div2">
        <div id="div3"></div>
      </div>
    </div>
    
    <script> 
    window.onload = function() {
    var oDiv3 = document.getElementById('div3');
        alert( oDiv3.offsetTop );
    }
    </script> 
    div3有定位父级则显示为到div2(父级)的距离
    offsetTop
    火狐,谷歌,360,opera:40 
    ie7-11:40
    ie7:如果自己没有定位,那么offsetTop是到body的距离 80 如果自己有定位,那么就是到定位父级的距离
    offsetLeft
    火狐,谷歌,360,opera:50 
    ie7-11:50
    ie7:如果自己没有定位,那么offsetTop是到body的距离 100 如果自己有定位,那么就是到定位父级的距离
     
     
  • 相关阅读:
    Python十大经典算法之选择排序
    在js中修改样式带 !important 的样式
    vscode 使用 ejs 语法有红色错误提示线
    mysql 报 'Host ‘XXXXXX’ is blocked because of many connection errors'
    字符编码
    Canal.adapter报错
    Windows 域控配置时间同步
    Docker清理日志脚本
    Docker快速部署clickhouse
    Windows批处理一键添加hosts文件
  • 原文地址:https://www.cnblogs.com/Xuman0927/p/5486736.html
Copyright © 2020-2023  润新知