1.offsetLeft,offsetTop

16122 단어 left
offsetLeft, offsetTop의 브라우저별 표현 차이
<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>document</title>

<style>

div{ padding:40px 50px;}

#div1 { background:red;}

#div2 { background:blue; position:relative;}

#div3 { background:orange;}

</style>

<script type="text/javascript">

    

    window.onload = function() {

        

        var oDiv3 = document.getElementById('div3');

        /*

            offsetLeft:                  (   )

                      offsetParent   

                        

                    offsetParent -> body

                    offsetLeft[html] -> html

                

                       

                    

                    ie7:        ,  offsetLeft[Top]  body   

                               ,            

                      :        

        */

        alert(oDiv3.offsetLeft);

    }

    

</script>

</head>



<body id="body">

    <div id="div1">

        <div id="div2">

            <div id="div3">

            

            </div>

        </div>

    </div>

</body>

</html>

원소의 결점을 페이지로 가져오는 절대 거리
<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>document</title>

<style>

div{ padding:40px 50px;}

#div1 { background:red;}

#div2 { background:blue; position:relative;}

#div3 { background:orange;}

</style>

<script type="text/javascript">

    

    window.onload = function() {

        

        var oDiv3 = document.getElementById('div3');

        //alert(oDiv3.offsetLeft);

        /*var iTop = 0;

        var obj = oDiv3;

        while(obj) {

            alert(obj.id + ':' + obj.offsetTop);

            iTop += obj.offsetTop;

            obj = obj.offsetParent;

            alert(obj + obj.id);

        }*/

        //alert(iTop);

        var p = getPos(oDiv3);

        alert(p.left);

        //                 

        function getPos(obj) {

            

            var pos = {left:0,top:0};

            

            while(obj) {

                pos.left += obj.offsetLeft;

                pos.top += obj.offsetTop;

                obj = obj.offsetParent;

            }

            return pos;

        }

        

    }

    

</script>

</head>



<body id="body">

    <div id="div1">

        <div id="div2">

            <div id="div3">

            

            </div>

        </div>

    </div>

</body>

</html>

좋은 웹페이지 즐겨찾기