offsetTop,clientX,clientTop,clientWidth,offsetWidth 座標,一次弄明白

來源:互聯網
上載者:User

這幾個屬性都是IEFirefox完全相容的,不多說,看我測試結果便知:

【源碼如下】

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

    <script type="text/javascript">
        function Position(eve) {
            var t = document.getElementById("div1");
            var e = eve || event;
            var DivOffsetTop = t.offsetTop; //=Top+MarginWidth=100+3=103
            var DivOffsetleft = t.offsetLeft; //Left+MarginWidth=50+3=53
            var EventTop = e.clientY;
            var EventLeft = e.clientX;
            var DivTop = t.style.top;
            var DivLeft = t.style.left;
            var DivClientTop = t.clientTop; //BorderWidth
            var DivClientLeft = t.clientLeft; //BorderWidth
            var DivClientWidth = t.clientWidth; //Width+2PaddingWidth=200+2*11=222
            var DivClientHeight = t.clientHeight; //Height+2PaddingWidth=300+2*11=322
            var DivOffsetWidth = t.offsetWidth; //=2BorderWidth+Width+2PaddingWidth=2*7+200+2*11=236
            var DivOffsetHeight = t.offsetHeight; //=2BorderWidth+Height+2PaddingWidth=2*7+300+2*11=336
            var BodyClientTop = document.body.clientTop;
            var BodyClientLeft = document.body.clientLeft;
            var DivWidth = t.style.width;
            var DivHeight = t.style.height;
            var DivBorderWidth = t.style.borderWidth;
            var DivPaddingWidth = t.style.padding;
            var DivMaginWidth = t.style.margin;
            var R = "DivOffsetleft:" + DivOffsetleft + "--DivOffsetTop:" + DivOffsetTop; //103 53
            R += "\nEventLeft:" + EventLeft + "--EventTop:" + EventTop; //>=103 >=53
            R += "\nDivLeft:" + DivLeft + "--DivTop:" + DivTop; //100px 50px
            R += "\nDivClientLeft:" + DivClientLeft + "--DivClientTop:" + DivClientTop; //7 7
            R += "\nBodyClientLeft:" + BodyClientLeft + "--BodyClientTop:" + BodyClientTop; //5 5
            R += "\nDivClientWidth:" + DivClientWidth + "--DivClientHeight:" + DivClientHeight; //222 322
            R += "\nDivWidth:" + DivWidth + "--DivHeight:" + DivHeight; //200 300
            R += "\nDivOffsetWidth:" + DivOffsetWidth + "--DivOffsetHeight:" + DivOffsetHeight; //236 336
            R += "\nDivBorderWidth:" + DivBorderWidth //7px
            R += "\nDivMaginWidth:" + DivMaginWidth //3px
            alert(R);
        }
    </script>

</head>
<body>
    <input type="button" id="btn" value="測試位置" />
    <br />
    <div id="div1"line-height: normal; ">
        top: 50px; left: 100px; border: 7px solid red; padding: 11px; margin: 3px;" onmousedown="Position(event)">
        這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容
        這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容
        這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容 這是文章內容
    </div>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.