zoukankan      html  css  js  c++  java
  • Jquery中的offset()和position()深入剖析(元素定位)

    先看看这两个方法的定义。
    offset():
    获取匹配元素在当前视口的相对偏移。
    返回的对象包含两个整形属性:top 和 left。此方法只对可见元素有效。
    position():
    获取匹配元素相对父元素的偏移。
    返回的对象包含两个整形属性:top 和 left。为精确计算结果,请在补白、边框和填充属性上使用像素单位。此方法只对可见元素有效。
    真的就这么简单吗?实践出真知。
    先来看看在jquery框架源码里面,是怎么获得position()的:

     1 // Get *real* offsetParent 
     2 var offsetParent = this.offsetParent(), 
     3 // Get correct offsets 
     4 offset = this.offset(), 
     5 parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset(); 
     6 // Subtract element margins 
     7 // note: when an element has margin: auto the offsetLeft and marginLeft 
     8 // are the same in Safari causing offset.left to incorrectly be 0 
     9 offset.top -= num( this, 'marginTop' ); 
    10 offset.left -= num( this, 'marginLeft' ); 
    11 // Add offsetParent borders 
    12 parentOffset.top += num( offsetParent, 'borderTopWidth' ); 
    13 parentOffset.left += num( offsetParent, 'borderLeftWidth' ); 
    14 // Subtract the two offsets 
    15 results = { 
    16 top: offset.top - parentOffset.top, 
    17 left: offset.left - parentOffset.left 
    18 };


     
    <!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=gb2312" /> 
    <title>offset和position测试1</title> 
    <script type="text/javascript" src="http://img.jb51.net/jslib/jquery/jquery-1.3.2.js"></script> 
    <style type="text/css"> 
    body{margin:15px;960px;} 
    .parent{ 
    border:3px solid #ccc; 
    600px; 
    height:300px; 
    margin-left:55px; 
    padding:25px; 
    } 
    .child{ 
    background:#666; 
    200px; 
    height:200px; 
    
    color:#fff; 
    } 
    .copyright{ 
    position:absolute; 
    right:0; 
    } 
    </style> 
    <script type="text/javascript"> 
    $(document).ready(function(){ 
    $(".child").each(function(){ 
    var text = "position().left="+$(this).position().left; 
    text +="<br>position().top="+$(this).position().top; 
    text +="<br>offset().left="+$(this).offset().left; 
    text +="<br>offset().top="+$(this).offset().top; 
    text +="<br>其parent的offset().top="+$(this).parents(".parent").offset().top; 
    text +="<br>其parent的offset().left="+$(this).parents(".parent").offset().left; 
    $(this).html(text); 
    }); 
    
    }); 
    </script> 
    </head> 
    <body> 
    
    <div class="parent" style="float:right"> 
    父容器的position是默认值,也就是static,子容器的position为默认值,也是static,这个时候,offset和position值相同<br><br><br> 
    <div class="child"></div> 
    </div> 
    <div style="clear:both"></div> 
    <br> 
    <div class="parent" style="position:relative"> 
    父容器的position是相对定位,也就是ralative,子容器的position为默认值,也是static,这个时候,offset和position值不同 
    <div class="child"></div> 
    </div> 
    <br> 
    <div style="position:absolute;padding:15px;border:3px solid #ff0000;"> 
    <div class="parent"> 
    父容器的position是绝对定位,也就是absolute,子容器的position为默认值,也是static,这个时候,offset和position值不同 
    <div class="child"></div> 
    </div> 
    </div> 
    
    <div class="copyright"><a href="http://www.playgoogle.com">©playgoogle.com</a></div> 
    </body> 
    </html> 


    通过test1页面测试的结果可以得出这个结论:
    使用position()方法时事实上是把该元素当绝对定位来处理,获取的是该元素相当于最近的一个拥有绝对或者相对定位的父元素的偏移位置。
    使用position()方法时如果其所有的父元素都为默认定位(static)方式,则其处理方式和offset()一样,是当前窗口的相对偏移
    使用offset()方法不管该元素如何定位,也不管其父元素如何定位,都是获取的该元素相对于当前视口的偏移
    知道了这些特点,我们应该如何来合理的使用position()和offset()呢?
    就我个人的经验,通常获取一个元素(A)的位置是为了让另外的一个元素(B)正好出现在A元素的附近。通常有2种情况:
    1.要显示的元素B存放在DOM的最顶端或者最底端(即其父元素就是body).这个时候用offset()是最好的。示例验证:

     1  
     2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     3 <html xmlns="http://www.w3.org/1999/xhtml"> 
     4 <head> 
     5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
     6 <title>offset和position测试1</title> 
     7 <script type="text/javascript" src="http://img.jb51.net/jslib/jquery/jquery-1.3.2.js"></script> 
     8 <style type="text/css"> 
     9 body{margin:15px;960px;} 
    10 .parent{ 
    11 border:3px solid #ccc; 
    12 600px; 
    13 height:300px; 
    14 margin-left:55px; 
    15 padding:25px; 
    16 } 
    17 input{ 
    18 height:25px; 
    19 float:right; 
    20 } 
    21 .copyright{ 
    22 position:absolute; 
    23 right:0; 
    24 } 
    25 .tip{ 
    26 border:3px dotted #669900; 
    27 background:#eee; 
    28 200px; 
    29 height:40px; 
    30 position:absolute; 
    31 display:none; 
    32 } 
    33 </style> 
    34 <script type="text/javascript"> 
    35 $(document).ready(function(){ 
    36 $("input").bind("click",function(){ 
    37 $(".tip").css({ 
    38 left:$(this).offset().left+"px", 
    39 top:$(this).offset().top+25+"px" 
    40 }); 
    41 $(".tip").toggle(); 
    42 }); 
    43 }); 
    44 </script> 
    45 </head> 
    46 <body> 
    47 用offset, 该例中元素B在DOM的最下面,也就是,其父容器为body 
    48 <div class="parent" style="position:absolute;left:150px"> 
    49 父元素是绝对定位的 
    50 <input type="button" value="点击我,正好在我的下面显示元素B,且左边和我对齐"> 
    51 </div> 
    52 <br><br><br><br><br><br><br><br> 
    53 <div class="tip"> 
    54 我是元素B<br> 
    55 现在用的是offset 
    56 </div> 
    57 </body> 
    58 </html> 


    用position无法正常显示的例子

     1  
     2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     3 <html xmlns="http://www.w3.org/1999/xhtml"> 
     4 <head> 
     5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
     6 <title>offset和position测试1</title> 
     7 <script type="text/javascript" src="http://img.jb51.net/jslib/jquery/jquery-1.3.2.js"></script> 
     8 <style type="text/css"> 
     9 body{margin:15px;960px;} 
    10 .parent{ 
    11 border:3px solid #ccc; 
    12 600px; 
    13 height:300px; 
    14 margin-left:55px; 
    15 padding:25px; 
    16 } 
    17 input{ 
    18 height:25px; 
    19 float:right; 
    20 } 
    21 .copyright{ 
    22 position:absolute; 
    23 right:0; 
    24 } 
    25 .tip{ 
    26 border:3px dotted #669900; 
    27 background:#eee; 
    28 200px; 
    29 position:absolute; 
    30 display:none; 
    31 } 
    32 </style> 
    33 <script type="text/javascript"> 
    34 $(document).ready(function(){ 
    35 $("input").bind("click",function(){ 
    36 $(".tip").css({ 
    37 left:$(this).position().left+"px", 
    38 top:$(this).position().top+25+"px" 
    39 }); 
    40 $(".tip").toggle(); 
    41 }); 
    42 }); 
    43 </script> 
    44 </head> 
    45 <body> 
    46 用position的例子, 该例中元素B的在DOM的最下面,也就是,其父容器为body 
    47 <div class="parent" style="position:absolute;left:150px"> 
    48 父元素是绝对定位的 
    49 <input type="button" value="点击我,正好在我的下面显示元素B,且左边和我对齐"> 
    50 </div> 
    51 <br><br><br><br><br><br><br><br> 
    52 <div class="tip"> 
    53 我是元素B<br> 
    54 现在用的是position<br> 
    55 你会发现我现在的位置不正确 
    56 </div> 
    57 </body> 
    58 </html> 


    在以上两个例子中,元素B都存放在Dom 结构的最下面,由于其父元素就是BODY,所以,不管元素A如何定位,只要找的A相当与整个窗口的偏移位置,就可以解决问题。

    2.若要显示的元素B存放在元素A的同一父元素下(即B为A的兄弟节点),这个时候使用position() 是最合适的。
    用offset 正常显示的例子

     1  
     2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     3 <html xmlns="http://www.w3.org/1999/xhtml"> 
     4 <head> 
     5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
     6 <title>offset和position测试1</title> 
     7 <script type="text/javascript" src="http://img.jb51.net/jslib/jquery/jquery-1.3.2.js"></script> 
     8 <style type="text/css"> 
     9 body{margin:15px;960px;} 
    10 .parent{ 
    11 border:3px solid #ccc; 
    12 600px; 
    13 height:300px; 
    14 margin-left:55px; 
    15 padding:25px; 
    16 } 
    17 input{ 
    18 height:25px; 
    19 float:right; 
    20 } 
    21 .copyright{ 
    22 position:absolute; 
    23 right:0; 
    24 } 
    25 .tip{ 
    26 border:3px dotted #669900; 
    27 background:#eee; 
    28 200px; 
    29 position:absolute; 
    30 display:none; 
    31 } 
    32 </style> 
    33 <script type="text/javascript"> 
    34 $(document).ready(function(){ 
    35 $("input").bind("click",function(){ 
    36 $(".tip").css({ 
    37 left:$(this).position().left+"px", 
    38 top:$(this).position().top+25+"px" 
    39 }); 
    40 $(".tip").toggle(); 
    41 }); 
    42 }); 
    43 </script> 
    44 </head> 
    45 <body> 
    46 用position, 该例中元素B在button按钮的附近,也就是,元素B和button按钮有共同的父容器 
    47 <div class="parent" style="position:absolute;left:150px"> 
    48 父元素是绝对定位的 
    49 <input type="button" value="点击我,正好在我的下面显示元素B,且左边和我对齐"> 
    50 <div class="tip"> 
    51 我是元素B<br> 
    52 现在用的是position, 
    53 我和按钮的父亲元素相同<br> 
    54 我能按要求正常显示 
    55 </div> 
    56 </div> 
    57 <br><br><br><br><br><br><br><br> 
    58 </body> 
    59 </html> 
    View Code


    用offset五法正常显示的例子

     1  
     2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     3 <html xmlns="http://www.w3.org/1999/xhtml"> 
     4 <head> 
     5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
     6 <title>offset和position测试1</title> 
     7 <script type="text/javascript" src="http://img.jb51.net/jslib/jquery/jquery-1.3.2.js"></script> 
     8 <style type="text/css"> 
     9 body{margin:15px;960px;} 
    10 .parent{ 
    11 border:3px solid #ccc; 
    12 600px; 
    13 height:300px; 
    14 margin-left:55px; 
    15 padding:25px; 
    16 } 
    17 input{ 
    18 height:25px; 
    19 float:right; 
    20 } 
    21 .copyright{ 
    22 position:absolute; 
    23 right:0; 
    24 } 
    25 .tip{ 
    26 border:3px dotted #669900; 
    27 background:#eee; 
    28 200px; 
    29 position:absolute; 
    30 display:none; 
    31 } 
    32 </style> 
    33 <script type="text/javascript"> 
    34 $(document).ready(function(){ 
    35 $("input").bind("click",function(){ 
    36 $(".tip").css({ 
    37 left:$(this).offset().left+"px", 
    38 top:$(this).offset().top+25+"px" 
    39 }); 
    40 $(".tip").toggle(); 
    41 }); 
    42 }); 
    43 </script> 
    44 </head> 
    45 <body> 
    46 用offset, 该例中元素B在button按钮的附近,也就是,元素B和button按钮有共同的父容器 
    47 <div class="parent" style="position:absolute;left:150px"> 
    48 父元素是绝对定位的 
    49 <input type="button" value="点击我,正好在我的下面显示元素B,且左边和我对齐"> 
    50 
    51 <div class="tip"> 
    52 我是元素B<br> 
    53 现在用的是offset, 
    54 我和按钮的父亲元素相同 
    55 我无法按要求正常显示 
    56 </div> 
    57 </div> 
    58 <br><br><br><br><br><br><br><br> 
    59 </body> 
    60 </html> 
    View Code


    综上所述,应该使用position()还是offset()取决于你被控制的元素B DOM所在的位置。

  • 相关阅读:
    Word中如何删除目录页的页码
    在java程序代码中打开文件
    Web程序报错:Error instantiating servlet
    将日期类型数据写入到数据库中
    《将博客搬至CSDN》
    软件测试工程师常见的面试题
    我对需求文档的理解
    简单的学生管理系统,实现增删改查
    如何求两个数的最大公约数
    【转载】LoadRunner监控window系统各项指标详解
  • 原文地址:https://www.cnblogs.com/-lpf/p/4921542.html
Copyright © 2011-2022 走看看