offset

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            .redDiv{
                width: 500px;
                height: 500px;
                background-color: red;
                border: 50px solid;
                /*position: relative;*/
            }
            .blueDiv{
                width: 200px;
                height: 200px;
                background-color: blue;
                /*margin-left: 10px;*/
                /*position: absolute;
                left: 10px;*/
                /*float: left;*/
                display: inline-block;
                border: 1px solid royalblue;
                /*padding: 50px;*/
                /*background-clip: content-box;*/
            }
            .yellowDiv{
                width: 100px;
                height: 100px;
                background-color: yellow;
                /*position: absolute;
                left: 250px;*/
                /*float: left;*/
                display: inline-block;
            }
        </style>
    </head>
    <body>
        <div class="redDiv">
            <div class="blueDiv"></div>
            <div class="yellowDiv"></div>
        </div>
    </body>
    <script type="text/javascript">
        var redDiv=document.querySelector(".redDiv");
        var blueDiv=document.querySelector(".blueDiv");
        var yellowDiv=document.querySelector(".yellowDiv");
        //offsetLeft是自身的左邊距離定位父級的左邊的距離。
        //如果沒有定位父級,則以窗口左邊為準。
        console.log(blueDiv.offsetLeft);
        //client是自身邊框的寬度
        console.log(blueDiv.clientLeft);
        console.log(yellowDiv.offsetLeft);
        
        console.log(redDiv.offsetWidth);
        console.log(redDiv.clientWidth);
    </script>
</html>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容