设为首页收藏本站

就爱编程论坛

 找回密码
 注册

人人连接登陆

无需注册,直接登录

用新浪微博连接

一步搞定

QQ登录

只需一步,快速开始

查看: 802|回复: 0
打印 上一主题 下一主题

getBoundingClientRect的用法 [复制链接]

Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    无聊
    2025-5-27 03:37:20
  • 签到天数: 366 天

    [LV.9]以坛为家II

    论坛先锋 学习至圣 荣誉成员 论坛元老 活跃之星 终极领袖

    我玩的应用:

    跳转到指定楼层
    楼主
    发表于 2011-7-8 20:22:19 |只看该作者 |倒序浏览
    先上测试代码
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "
    http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>管理系统</title>
    </head>

    <body style="" ><!--7699c7-->
    <div id="w3124" style="border:1px solid #ff0000;position:relative;top:200px;left:400px;width:400px;">

    </div>
    <SCRIPT>
    alert(document.getElementById("w3124").getBoundingClientRect().top);
    </SCRIPT>
    </body>
    </html>


    下面是alert结果
    IE、FF、Chrome:208
    IE内核的Maxthon:215
    IE内核的TheWorld:217

    当body加上margin:0;padding:0 的时候IE、FF、Chrome、Maxthon下均为200,而只有TheWorld为202
    然后把HTML代码头部的DOCTYPE声明去掉的时候FF、Chrome、Maxthon值均为200,而IE由于进入Quirks模式,此 时的值为202,而TheWorld仍为为202
    结论
    FF、Chrome、Maxthon为始终坚持标准模式 (Standards Mode),IE在加了声明后也进入标准模式(Standards Mode),只有TheWorld始终坚持Quirks模式,万恶!
    建议
    为了兼容把body加上margin:0;padding:0,注意 加DOCTYPE声明(有了它IE还是挺听话的)

    引用自:www.cnblogs.com/janoyu/archive/2009/05/21/1486657.html






    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"
    />
    <title>Demo</title>
    </head>
    <body style="width:2000px; height:1000px;">
    <div id="demo" style="position:absolute; left:518px; right:100px; width:500px; height:500px; background:#CC0000; top: 114px;">Demo为了方便就直接用绝对定位的元素</div>
    </body>
    </html>
    <script>
    document.getElementById(
    'demo').onclick=function (){
            
    if (document.documentElement.getBoundingClientRect) {
                alert(
    "left:"+this.getBoundingClientRect().left)
                alert(
    "top:"+this.getBoundingClientRect().top)
                alert(
    "right:"+this.getBoundingClientRect().right)
                alert(
    "bottom:"+this.getBoundingClientRect().bottom)
                
    var
    X=
    this.getBoundingClientRect().left+document.documentElement.scrollLeft;
                
    var Y =
    this.getBoundingClientRect().top+document.documentElement.scrollTop;
                alert(
    "Demo的位置是X:"+X+";Y:"+Y)
            }
    }
    </script>
    有了这个方法,获取页面元素的位置就简单多了,

    var X=
    this.getBoundingClientRect().left+document.documentElement.scrollLeft;
                
    var Y =this.getBoundingClientRect().top+document.documentElement.scrollTop;
    附件: 你需要登录才可以下载或查看附件。没有帐号?注册 人人连接登陆
    分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    分享分享0 收藏收藏0 支持支持0 反对反对0 分享到人人 转发到微博
    [img=http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=fRUcHhYWGAQ9GxIFEBwUEVMeEhA]http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_02.png[/img]

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册 人人连接登陆

    晴云孤魂's Blog|就爱编程搜帖|手机版|Archiver|就爱编程论坛     

    GMT+8, 2025-7-4 21:13 , Processed in 0.106405 second(s), 31 queries .

    Powered by Discuz! X2

    © 2001-2011 Comsenz Inc.

    回顶部