zoukankan      html  css  js  c++  java
  • DOM and CSS positioning

    offsetXXX、scrollXXX 与clientXXX的区别与联系

    offsetTop returns the distance of the current element relative to the top of the offsetParent node.

    offsetLeft Returns the number of pixels that the upper left corner of the current element is offset to the left within the offsetParent node.

    offsetHeight returns height of an element relative to the element's offsetParent.

    offsetHeight is part of the MSIE's DHTML object model. offsetHeight is not part of any W3C specification or technical recommendation.

    Notes

    offsetHeight is a property of the DHTML object model which was first introduced by MSIE. It is sometimes referred to as an element's physical/graphical dimensions, or an element's border-box height.

    offsetParent的理解

    MDC中对offsetParent的介绍是这样的:

    offsetParent returns a reference to the object which is the closest (nearest in the containment hierarchy) positioned containing element. If the element is non-positioned, the nearest table cell or root element (html in standards compliant mode; body in quirks rendering mode) is the offsetParent. offsetParent returns null when the element has style.display set to "none". The offsetParent is useful because offsetTop and offsetLeft are relative to its padding edge.

    在td中的元素会把第一个绝对/相对定位的hierarchy parent当作offsetParent,如果没有找到需要分三种情况讨论

    1. 如果该元素没有绝对/相对定位,则会把td当作offsetParent
    2. 如果该元素绝对/相对定位并且table没有绝对/相对定位,则会把body当作offsetParent
    3. 如果该元素绝对/相对定位并且table绝对/相对定位,则会把table当作offsetParent

    Javascript code:

            function checkoffsetParent() {
               
    var div = document.getElementById("parentdiv");
                alert(div.offsetParent.tagName);
                div
    = document.getElementById("sondiv");
                alert(div.offsetParent.tagName);
               
    var td = document.getElementsByTagName("td")[0];
                alert(td.offsetParent.tagName);
            }

    1.

    <body onload="checkoffsetParent()">
       
    <table border="1" align="right">
           
    <tr>
               
    <td id="oCell" style="position: fixed; top: 200px; left: 300px;">
                   
    <div id="parentdiv" style="position: relative">parentdiv
                        <div id="sondiv">sondiv</div>
                   
    </div>
               
    </td>
           
    </tr>
       
    </table>
    </body>

    运行结果:

    parentdiv.offsetParent.tagName IS "body"
    sondiv.offsetParent.id     IS "parentdiv"

    2.

        <table border="1" align="right">
           
    <tr>
               
    <td id="oCell">
                   
    <div id="parentdiv" style="position: relative">
                        parentdiv
                       
    <div id="sondiv" style="position: relative">
                            sondiv
    </div>
                   
    </div>
               
    </td>
           
    </tr>
       
    </table>

    运行结果:

    parentdiv.offsetParent.tagName IS "body"
    sondiv.offsetParent.id     IS "parentdiv"

    3.

        <table border="1" align="right">
           
    <tr>
               
    <td id="oCell">
                   
    <div id="parentdiv">
                        parentdiv
                       
    <div id="sondiv" style="position: relative">
                            sondiv
    </div>
                   
    </div>
               
    </td>
           
    </tr>
       
    </table>

    运行结果:

    parentdiv.offsetParent.tagName IS "TD"
    sondiv.offsetParent.id     IS "body"

    这个结果是比较有意思的,但它也是符合上面总结的规则的。

    4.
        <table border="1" align="right">
           
    <tr>
               
    <td id="oCell">
                   
    <div id="parentdiv">
                        parentdiv
                       
    <div id="sondiv">
                            sondiv
    </div>
                   
    </div>
               
    </td>
           
    </tr>
       
    </table>

    运行结果:

    parentdiv.offsetParent.tagName IS "TD"
    sondiv.offsetParent.id     IS "TD"

    5.

        <table border="1" align="right" style="position: relative">
           
    <tr>
               
    <td id="oCell">
                   
    <div id="parentdiv" style="position: relative">
                        parentdiv
                       
    <div id="sondiv" style="position: relative">
                            sondiv
    </div>
                   
    </div>
               
    </td>
           
    </tr>
       
    </table>

    运行结果:

    parentdiv.offsetParent.tagName IS "Table"
    sondiv.offsetParent.id     IS "pareentdiv"

    Reference:http://www.cnblogs.com/NRabbit/archive/2009/01/11/1736202.html

    http://www.cnblogs.com/panjun-Donet/articles/1294033.html

  • 相关阅读:
    webapi 中使用 protobuf
    apache httpclient cache 实现可缓存的http客户端
    编译nginx时提示undefined reference to 'pcre_free_study' 的问题及解决
    深入理解JVM内存回收机制(不包含垃圾收集器)
    从JDK源码理解java引用
    Buffer的创建及使用源码分析——ByteBuffer为例
    二叉树的子结构、深度以及重建二叉树
    数据结构——树与二叉树的遍历
    Java NIO之Buffer的使用
    Java多线程之synchronized详解
  • 原文地址:https://www.cnblogs.com/whyandinside/p/1848492.html
Copyright © 2011-2022 走看看