zoukankan      html  css  js  c++  java
  • 2 获取元素在页面中的位置

    获取元素在页面中的位置
    绝对位置:到body的距离
    相对位置:到视口的距离


    本身定位为fixed
    ==> offsetParent:null(不是火狐)
    offsetTop和offsetLeft也是参照于body的
    ==> offsetParent:body(火狐)
    本身定位不为fixed
    父级没有定位
    ==> offsetParent:body
    父级有定位
    ==> offsetParent:定位父级

    function getPointAb(node){
                    //while循环叠加offsetParent的offsetTop和offsetLeft
                    var x =0;
                    var y = 0;
                    while(node){
                        x+=node.offsetLeft;
                        y+=node.offsetTop;
                        
                        node = node.offsetParent;
                    }
                    
                    return {x:x,y:y};
                }
    封装函数
    <!DOCTYPE html>
    <html id="html">
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style type="text/css">
                *{
                    margin: 0;
                    padding: 0;
                }
                html,body{
                    height: 100%;
                    overflow: hidden;
                }
                #wrap{position: absolute;background: pink;left: 200px;top: 200px;}
                #inner1{position: absolute;background: deeppink;left: 150px;top: 150px;}
                div{
                     200px;
                    height: 200px;
                }
            </style>
        </head>
        <body id="body" >
            <div id="wrap">wrap
                <div id="inner1">
                    inner1
                </div>
            </div>
        </body>
        <script type="text/javascript">
            /*
             获取元素在页面中的位置
                绝对位置:到body的距离
                相对位置:到视口的距离
                
                
                本身定位为fixed
                        ==> offsetParent:null(不是火狐)
                                offsetTop和offsetLeft也是参照于body的
                        ==> offsetParent:body(火狐)
                本身定位不为fixed
                        父级没有定位
                            ==> offsetParent:body
                        父级有定位
                            ==> offsetParent:定位父级
                
                
            */
            window.onload=function(){
                var inner1 = document.querySelector("#inner1");
                var point = getPointAb(inner1);
                console.log(point);
                
                
                //boder margin会影响这个函数的取值
                function getPointAb(node){
                    //while循环叠加offsetParent的offsetTop和offsetLeft
                    var x =0;
                    var y = 0;
                    while(node){
                        x+=node.offsetLeft;
                        y+=node.offsetTop;
                        
                        node = node.offsetParent;
                    }
                    
                    return {x:x,y:y};
                }
            }
        </script>
    </html>
    View Code




    */

  • 相关阅读:
    python-条件判断
    获取网卡名称
    vSphere Client安装
    python远程执行命令
    xorm操作
    httpd服务安装配置
    error: failed to push some refs to 'git@gitee.com:xxxx'
    三种获取数据的方法fetch和ajax和axios
    react组件的生命周期
    react在移动端的自适应布局
  • 原文地址:https://www.cnblogs.com/hack-ing/p/11865905.html
Copyright © 2011-2022 走看看