<div align="center">
</div>如何通过js获得上面这个层的位置

解决方案 »

  1.   


    /**
     * 坐标
     * @param x
     * @param y
     * @return
     */
    function CPos(x, y)
    {
        this.x = x;
        this.y = y;
    }
    /**
     * 得到对象的相对浏览器的坐标
     * @param ATarget
     * @return
     */
    function GetObjPos(ATarget)
    {
        var target = ATarget;
        var pos = new CPos(target.offsetLeft, target.offsetTop);
        
        var target = target.offsetParent;
        while (target)
        {
            pos.x += target.offsetLeft;
            pos.y += target.offsetTop;
            
            target = target.offsetParent
        }
        return pos;
    }
      

  2.   


    <div id="test" style="position:absolute; left:100px; top:200px;">123</div>
    <script>/**
         * 坐标
         * @param x
         * @param y
         * @return
         */
        function CPos(x, y)
        {
            this.x = x;
            this.y = y;
        }
        /**
         * 得到对象的相对浏览器的坐标
         * @param ATarget
         * @return
         */
        function GetObjPos(ATarget)
        {
            var target = ATarget;
            var pos = new CPos(target.offsetLeft, target.offsetTop);
            
            var target = target.offsetParent;
            while (target)
            {
                pos.x += target.offsetLeft;
                pos.y += target.offsetTop;
                
                target = target.offsetParent
            }
            return pos;
        }

    var obj =  document.getElementById('test')
    alert(GetObjPos(obj)['y']) //y坐标
    alert(GetObjPos(obj)['x']) //x坐标

    </script>
    不错
      

  3.   

    <div align="center" id="div1">
    </div>
     <script>
    var div1=document.getElementById("div1");
    var divPos=GetObjPos(div1);
    alert(divPos.x);
    alert(divPos.y);
    </script>
      

  4.   

    简单点的 可以通过document.getElementById("DIV的ID").style.Top(Left)来获取元素据浏览器上端(左端)距离 即xy坐标位置
      

  5.   

    JS DOM中getElementByTagName("div")获取标签元素,object.getBoundingClientRect()方法获取坐标,通过left和top属性。
    参考下这个,希望对你有帮助http://www.jz123.cn/text/0626163.html