• js获取DIV的位置坐标的三种方法


    js获取DIV的位置坐标的三种方法

    2013-11-04 16:12:53  阅读数 1032更多

    分类专栏: javascript

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

    本文链接:https://blog.csdn.net/helloliuhai/article/details/14126593

     方法一:

    var odiv=document.getElementByIdx_x_x('divid');

    alert(odiv.getBoundingClientRect().left);

    alert(odiv.getBoundingClientRect().top);

    方法二:

     function CPos(x, y)

     {

         this.x = x;

         this.y = y;

      }

        

     function GetObjPos(ATarget)

     {

         var target = ATarget;

         var pos = new CPos(target.offsetLeft, target.offsetTop);      

         var target = target.offsetParent;  //(1)

         while (target)

         {

             pos.x += target.offsetLeft;

             pos.y += target.offsetTop;     

             target = target.offsetParent  //(2)

         }

         return pos;

     }

          

    var obj =  document.getElementByIdx_x_x('divid')  

    alert(GetObjPos(obj)['x']) //x坐标

    alert(GetObjPos(obj)['y']) //y坐标

     

    (1)和(2)这两行代码是什么意思,不是很清楚。。。貌似删除后可以的。

    方法三:

    function getElementPos(elementId){    

        var ua = navigator.userAgent.toLowerCase();    

        var isOpera = (ua.indexOf('opera') != -1);    

        var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof    

        var el = document.getElementByIdx_x_x_x_x(elementId);    

        if (el.parentNode === null || el.style.display == 'none') {    

            return false;    

        }    

        var parent = null;    

        var pos = [];    

        var box;    

        if (el.getBoundingClientRect) //IE    

        {    

            box = el.getBoundingClientRect();    

            var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);    

            var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);    

            return {    

                x: box.left + scrollLeft,    

                y: box.top + scrollTop    

            };    

        }    

        else     

            if (document.getBoxObjectFor) // gecko        

            {    

                box = document.getBoxObjectFor(el);    

                var borderLeft = (el.style.borderLeftWidth) ? parseInt(el.style.borderLeftWidth) : 0;    

                var borderTop = (el.style.borderTopWidth) ? parseInt(el.style.borderTopWidth) : 0;    

                pos = [box.x - borderLeft, box.y - borderTop];    

            }    

            else // safari & opera        

            {    

                pos = [el.offsetLeft, el.offsetTop];    

                parent = el.offsetParent;    

                if (parent != el) {    

                    while (parent) {    

                        pos[0] += parent.offsetLeft;    

                        pos[1] += parent.offsetTop;    

                        parent = parent.offsetParent;    

                    }    

                }    

                if (ua.indexOf('opera') != -1 || (ua.indexOf('safari') != -1 && el.style.position == 'absolute')) 

      

    {    

                    pos[0] -= document.body.offsetLeft;    

                    pos[1] -= document.body.offsetTop;    

                }    

            }    

        if (el.parentNode) {    

            parent = el.parentNode;    

        }    

        else {    

            parent = null;    

        }    

        while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') { // account for any scrolled 

      

    ancestors    

            pos[0] -= parent.scrollLeft;    

            pos[1] -= parent.scrollTop;    

            if (parent.parentNode) {    

                parent = parent.parentNode;    

            }    

            else {    

                parent = null;    

            }    

        }    

        return {    

            x: pos[0],    

            y: pos[1]    

        };    

      

    var xd = getElementPos("divid");

    alert(xd.x);

    alert(xd.y);

     

    注意:方法二和方法三中用到的offsetLeft和offsetTop属性所指定的父元素,将在offsetParent文章中做详细的介绍。

     

  • 相关阅读:
    POJ 3368.Frequent values
    HDOJ 1166.敌兵布阵
    javaWeb之文件下载
    javaWeb之文件上传
    centos系统下忘记了root密码怎么办?
    如何在centos下挂载与卸载磁盘
    验证码生成(java版本)
    javaweb怎么过滤乱码
    mysql修改默认字段大小
    我所知道的命名方式(软件)
  • 原文地址:https://www.cnblogs.com/grj001/p/12223699.html
Copyright © 2020-2023  润新知