关于table-cell 中 offsetParent的一些困惑

MDN 上关于 HTMLElement.offsetParent 的描述如下:

The **HTMLElement.offsetParent
** read-only property 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.displayset to "none". The offsetParent is useful because offsetTop and offsetLeft are relative to its padding edge.

关于其中的 the nearest table cell , 我做了一下验证,代码如下:

使用 table>tr>td

<!DOCTYPE html>
<html>
<head>
    <style>
    #test {
        margin: 10px;
        padding: 10px;
        width: 200px;
        border: 5px solid black;
    }
    </style>
</head>

<body onload="myFunction()">
    <table>
        <tr>
            <td>
                <div id="test">
                    <p>offsetParent is: <span id="demo"></span></p>
                </div>
            </td>
        </tr>
    </table>
    </div>
    <script>
    function myFunction() {
        var testDiv = document.getElementById("test");
        document.getElementById("demo").innerHTML = testDiv.offsetParent.tagName;
    }
    </script>
</body>
</html>
使用 table

使用 display:table-cell

但如果 使用 table-cell来模拟 单元格,结果就不一样了。

<!DOCTYPE html>
<html>

<head>
    <style>
    #test {
        margin: 10px;
        padding: 10px;
        width: 200px;
        border: 5px solid black;
    }
    .container {
        display: table-cell;
    }
    </style>
</head>

<body onload="myFunction()">
    <div class="container">
        <div id="test">
            <p>offsetParent is: <span id="demo"></span></p>
        </div>
    </div>
    <script>
    function myFunction() {
        var testDiv = document.getElementById("test");
        document.getElementById("demo").innerHTML = testDiv.offsetParent.tagName;
    }
    </script>
</body>

</html>

使用 display:table-cell

被包含元素使用 定位

如果被包含元素上使用了position ,则 offsetParent 也会变成 body.

<!DOCTYPE html>
<html>
<head>
    <style>
    #test {
        margin: 10px;
        padding: 10px;
        width: 200px;
        border: 5px solid black;
        position: relative;
    }
    </style>
</head>

<body onload="myFunction()">
    <table>
        <tr>
            <td>
                <div id="test">
                    <p>offsetParent is: <span id="demo"></span></p>
                </div>
            </td>
        </tr>
    </table>
    </div>
    <script>
    function myFunction() {
        var testDiv = document.getElementById("test");
        document.getElementById("demo").innerHTML = testDiv.offsetParent.tagName;
    }
    </script>
</body>
</html>
被包含元素使用定位

结论

  1. 如果被包含元素自身有定位
  2. 如果有定位祖先元素,则 offsetParent 是最近的有定位的祖先节点。
  3. 如果无定位祖先元素,则 offsetParent 是body。
  4. 如果被包含元素自身没有定位
  5. 如果有定位祖先元素(td table 也属于定位),则 offsetParent 是最近的有定位的祖先节点。
  6. 如果无定位祖先元素,则 offsetParent 是body。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,868评论 1 92
  • 张晓晗 导入部分不充分 观点出现在后 仪态控制不错 张天放 导入有些远--超市见香蕉 介绍饼图有误,不是利润表pr...
    夏日小宇阅读 56评论 0 0
  • 最近,天总是下雨。今天也是淅淅沥沥地下了一整天,虽然不大,但这秋雨却显得很缠绵。秋风夹杂着凉丝丝的雨滴打在身上,真...
    郑小赛同学阅读 339评论 0 2
  • 1 窗外又下起一阵蒙蒙细雨,王良喜欢这样的天气,雨水轻拍在路面的画面,偶尔溅起的涟漪,都让人很是舒心,王良觉得每一...
    风向北鸿阅读 475评论 0 0
  • 一点见解: Android事件分发机制(一) - 基本概念解释一点见解: Android事件分发机制(二) - 分...
    AssIstne阅读 1,143评论 4 2