offsetLeft, offsetTop以及postion().left , postion().top有神马区别

摘要:
˂!
<!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>
  for test,回答标题:
    offsetLeft是指当前元素的外边框到包含元素的内边框;
    position().left是指当前元素的margin(不包含margin)到定位元素的border-box(不包含border,但是包含padding),即pdding-box;
    都是从父级的padding-box开始的
</title>
<script src="http://zeptojs.com/zepto.js"></script>
</head>
<body>
    <style type="text/css">
        body {
            border:20px solid #CCC;
            margin:30px;
            padding:10px;
            background:#EEE;
            position:relative;
        }
        #test {
            width:800px;
            height:600px;
            margin:40px;
            padding:20px;
            border:5px solid #888;
            background:#F60;
            position:relative;
        }
        #test-div2{
            width:200px;
            background:#999;
            border:#fff solid 10px;
            padding:20px;
            margin:30px;
        
        }
    </style>
    <div id="test"></div>
    <div>
        <pre>    
        //在火狐和chrome和IE浏览器上的offsetLeft都是当前元素的marginLeft + offsetParent的paddingLeft,但是有一种情况下chrome的结果和[IE|FF]不一致,
        //那就是在offsetParent为body的情况下, 在chrome下offsetParent为body的话offsetLeft会算上body.borderWidth
        
        //通过zepto的el.position()获取的left和top值;            
        //这个是指从父级的contentBox到子元素的margin-box(没有包含margin);            
        $("#test-div2").position()
        Object {top: 242, left: 20}
        $("#test").position()
        Object {top: 40, left: 40}
        //因为是body,所以zepto计算的就有问题(好烂的解释方式),布局尽量清楚点,惹不起还怕躲不起吗么么哒;
        $("body").position()
        Object {top: -20, left: -20};
        
        //难懂的东西自己去写就好懂了,要么看了看了越看越晕的;
        知识点:通过zepto获取的 position() 返回的LEFT和TOP是相对父级元素的LEFT和TOP的值 .这个值是通过 getBoundingClientRect 的差值(父级的border + 父级的padding + 中间的各种宽度么么哒 + 子元素的margin) 减去 - 父级的border - 子元素的maring (符合一个元素的left或者是top是父级元素的content-box 到子元素的margin的距离);
        知识点:getBoundingClientRect()获取的也是相对界面的值,jQ和zepto的position实现就是根据getBoundingClientRect(),getBoundingClientRect是从元素的border-box开始的(包含border-box)到界面左上角的距离;
        知识点:chrome的开发者工作的实时查看margin,border,padding,content调试的颜色是从浅入深的;
            {
                margin : 橘色,
                border : 灰色,
                padding : 褐色,
                content : 偏蓝色
            }
            火狐
            {
                margin : 黄色,
                border : 偏黑色,
                padding : 紫色,
                content : 偏蓝色
            }
        </pre>
    </div>
    <div></div>
    <script>
        window.onload = function() {
            var test = document.getElementById("test");
            test.innerHTML = "<p>Browser:" + navigator.userAgent + "</p>" +
                "<p>offsetWidth:" + test.offsetWidth + "</p>" +
                "<p>offsetHeight:"+test.offsetHeight+"</p>"+
                "<p>offsetLeft:"+test.offsetLeft+"</p>"+
                "<p>offsetTop:"+test.offsetTop+"</p>"+
                "<br><div id="test-div2"></div>";
                //在父元素是body的情况下;
                //chrome下的:
                    //offsetLeft为  :  子元素的margin-left  + body.borderLeft + body.paddingLeft
                //ff下得 :
                    //offsetLeft为  :  子元素的margin-left + body.paddingLeft
                /*
                    在IE8/9/10及Chrome中,offsetLeft = (body的margin-left)+(body的border-width)+(body的padding-left)+(当前元素的margin-left);    
                      在FireFox中,offsetLeft = (body的margin-left)+(body的padding-left)+(当前元素的margin-left)
                */
            var div2 = document.getElementById("test-div2");
            div2.innerHTML = "<p>Browser:" + navigator.userAgent + "</p>" +
                "<p>"+div2.offsetParent.tagName+"</p>"+
                "<p>offsetWidth:" + div2.offsetWidth + "</p>" +
                "<p>offsetHeight:"+div2.offsetHeight+"</p>"+
                "<p>offsetLeft:"+div2.offsetLeft+"</p>"+
                "<div   id='div3'>div</div>";
                //在父元素不是body的情况下;
                //chrome下得 :
                    //offsetLeft也为的margin-left(包含margin-left)到相对父级的padding-left(包含padding-left);
                    //CODE用代码表示  ==〉〉  offsetLeft = offset.margin-left + el.padding-left
                //ff下得 :
                    //offsetLeft为当前元素的margin-left(包含margin-left)到相对父级的padding-left(包含padding-left);
                    //CODE用代码表示  ==〉〉offsetLeft = offset.margin-left + el.padding-left
        };
    </script>
</body>
</html>
        网页可见区域宽: W3C标准:document.documentElement.clientWidth ; IE低版本:document.body.clientWidth;
        (
        	PS: document.documentElement.clientWidth是指不包含滚动条的宽度,
        	window.innerWidth是包含滚动条长度的用户宽度,
            window.outerWidth是指window.innerWidth加上包含外部边框的长度;
            如果一个元素出现了垂直滚动条,那么这个元素的clientHeight还是offsetHeight-border*2, 只有父级的clientHeight才是可视区的高度;
            如果该元素隐藏了,就什么值都获取不到了;
        )
网页可见区域高: W3C标准:document.documentElement.clientHeight ; IE低版本:document.body.clientHeight;
(PS: 因为现代浏览器的html为用户界面, body元素是一个正常的元素, 滚动时出现在html元素的, 在IE低版本下面的html是隐藏的,body是出现滚动的元素);
网页可见区域宽: W3C标准:document.body.offsetWidth (包括border和padding的宽);
知识点==>> W3C标准:offsetWidth === contentWidth(内容宽) + border-width*2 + padding-width*2; 网页可见区域高: 同上; W3C标准下的width === 内容区域的宽; IEquirk模式width === 包含paddingw和border的宽度;
用户高度: clientHeight不包含margin和border的高度, 即为padding值加上content-height;
clientLeft: 其实这个就是border-left的长度, 跟margin居然没有关系,感觉没有存在的意义(WEBKIT测试结果)

  

<!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>计算后样式</title>
</head>
<body>


    <style type="text/css">
        body {
            border:20px solid #CCC;
            margin:30px;
            padding:10px;
            background:#EEE;
            position:relative;
        }
        #test {
            800px;
            height:600px;
            margin:40px;
            padding:20px;
            border:5px solid #888;
            background:#F60;
            position:relative;
        }
        #test-div2{
            200px;
            background:#999;
            border:#fff solid 10px;
            padding:20px;
            margin:30px;
        
        }
    </style>
    <div id="test"></div>
    <script>
    
        window.onload = function() {
            var test = document.getElementById("test");
            test.innerHTML = "<p>Browser:" + navigator.userAgent + "</p>" +
                "<p>offsetWidth:" + test.offsetWidth + "</p>" +
                "<p>offsetHeight:"+test.offsetHeight+"</p>"+
                "<p>offsetLeft:"+test.offsetLeft+"</p>"+
                "<p>offsetTop:"+test.offsetTop+"</p>"+
                "<br><div id="test-div2"></div>";
            var div2 = document.getElementById("test-div2");
            div2.innerHTML = "<p>Browser:" + navigator.userAgent + "</p>" +
                "<p>"+div2.offsetParent.tagName+"</p>"+
                "<p>offsetWidth:" + div2.offsetWidth + "</p>" +
                "<p>offsetHeight:"+div2.offsetHeight+"</p>"+
                "<p>offsetLeft:"+div2.offsetLeft+"</p>"+
                "<div   id='div3'>div</div>";
        };
        /*现在chrome下测试所有的值;
            left,top:为元素的margin外边距到包含容器的padding的像素值;
            如果容器是body的话:
                offsetLeft包含了body的borderLeft;
            如果包含容器不是body的话:
                offsetLeft, offsetTop为元素的外边框边距到包含容器的内边框边距的像素值;
            clientLeft为borderLeft;
            没有滚动的情况下:
                clientWidth为元素的内容宽度加上两边的padding;
                clientHeight同理;
                没有滚动的情况下你不要计算scrollWidth,因为浏览器之间的定义不一样, 直接认为和clientWidth一样好了;
            有滚动的情况下:
                clientWidth为视口的宽度
                scrollWidth为内部滚动区域的长度;
                scrollLeft为隐去的左侧距离;            
        */
    </script>
    </script>
 </body>
</html>

免责声明:文章转载自《offsetLeft, offsetTop以及postion().left , postion().top有神马区别》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Python--day37--多进程Elasticsearch logstsh同步mysql数据到ES中下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Bucardo使用文档-lottu

官网地址 一、Bucardo介绍 Bucardo 是基于表复制的系统 Bucardo 可以实现PostgreSQL数据库的双master/多master的方案 Bucardo的核心是一个Perl守护进程,它侦听通知请求并对其进行操作,方法是连接到远程数据库并来回复制数据。 守护进程需要的所有特定信息都存储在主bucardo数据库中,包括复制所涉及的所有数据...

PCIe固态存储和HDD常见的硬盘性能对比测试

2周测试后,导致以下结果 MySQL-OLTP测试结果:(50表。每个表1000广域网数据,1000个线程) TPS:MySQL在PCIe固态存储上执行是在HDD上执行的5.63倍 writes:MySQL在PCIe固态存储上执行是在HDD上执行的5.58倍 reads:MySQL在PCIe固态存储上执行是在HDD上执行的5.55倍 R...

gcc/g++编译

1. gcc/g++在执行编译工作的时候,总共需要4步 (1).预处理,生成.i的文件[预处理器cpp] (2).汇编,将预处理后的文件转换成汇编语言,生成文件.s[编译器egcs] (3).编译,将汇编语言文件编译为目标代码(机器代码)生成.o的文件[汇编器as] (4).链接,将目标代码,生成可执行程序[链接器ld] [参数详解]   -x lang...

PowerMock+Junit4 Maven配置

       在单元测试中,我们往往想去独立地去测一个类中的某个方法,但是这个类可不是独立的,它会去调用一些其它类的方法和service,于是JMockit、PowerMock 和 Mockito就诞生了。最近在学习PowerMock时发现网上对于他的Maven配置说明不是很详细,导致在使用时会报错,显示ClassNotFoundException和NoC...

uboot的relocation原理详细分析

转自:http://blog.csdn.net/skyflying2012/article/details/37660265 最近在一直在做uboot的移植工作,uboot中有很多值得学习的东西,之前总结过uboot的启动流程,但uboot一个非常核心的功能没有仔细研究,就是uboot的relocation功能。 这几天研究下uboot的reloca...

label和input表单的height问题

(col-xs-是指占据container的多少,不是指占据body的多少) (line-height的单位还可以是%和em,其中%是指为字体的%多少倍) (em可以精确到小数点后三位,可以说是很方便了) 1.row中的label和input放在一起的话,高度要给label和input,不要直接给row 因为input会有自带的padding,如果给row...