jquery 中有两个获取元素位置的方法offset()和position(),这两个方法之间有什么异同?使用的时候应该注意哪些问题?什么时候使用offset(),什么时候又使用position()呢?

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

代码如下:
// Get *real* offsetParent
var offsetParent = this.offsetParent(),
// Get correct offsets
offset = this.offset(),
parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset();
// Subtract element margins
// note: when an element has margin: auto the offsetLeft and marginLeft
// are the same in Safari causing offset.left to incorrectly be 0
offset.top -= num( this, 'marginTop' );
offset.left -= num( this, 'marginLeft' );
// Add offsetParent borders
parentOffset.top += num( offsetParent, 'borderTopWidth' );
parentOffset.left += num( offsetParent, 'borderLeftWidth' );
// Subtract the two offsets
results = {
top: offset.top - parentOffset.top,
left: offset.left - parentOffset.left
};

点击下面的页面可以测试一下两个的区别:

 <!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;width:960px;}
.parent{
border:3px solid #ccc;
width:600px;
height:300px;
margin-left:55px;
padding:25px;
}
.child{
background:#666;
width: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()是最好的。示例验证:

 <!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;width:960px;}
.parent{
border:3px solid #ccc;
width:600px;
height:300px;
margin-left:55px;
padding:25px;
}
input{
height:25px;
float:right;
}
.copyright{
position:absolute;
right:0;
}
.tip{
border:3px dotted #669900;
background:#eee;
width:200px;
height:40px;
position:absolute;
display:none;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$("input").bind("click",function(){
$(".tip").css({
left:$(this).offset().left+"px",
top:$(this).offset().top+25+"px"
});
$(".tip").toggle();
});
});
</script>
</head>
<body>
用offset, 该例中元素B在DOM的最下面,也就是,其父容器为body
<div class="parent" style="position:absolute;left:150px">
父元素是绝对定位的
<input type="button" value="点击我,正好在我的下面显示元素B,且左边和我对齐">
</div>
<br><br><br><br><br><br><br><br>
<div class="tip">
我是元素B<br>
现在用的是offset
</div>
</body>
</html>

用position无法正常显示的例子

<!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;width:960px;}
.parent{
border:3px solid #ccc;
width:600px;
height:300px;
margin-left:55px;
padding:25px;
}
input{
height:25px;
float:right;
}
.copyright{
position:absolute;
right:0;
}
.tip{
border:3px dotted #669900;
background:#eee;
width:200px;
position:absolute;
display:none;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$("input").bind("click",function(){
$(".tip").css({
left:$(this).position().left+"px",
top:$(this).position().top+25+"px"
});
$(".tip").toggle();
});
});
</script>
</head>
<body>
用position的例子, 该例中元素B的在DOM的最下面,也就是,其父容器为body
<div class="parent" style="position:absolute;left:150px">
父元素是绝对定位的
<input type="button" value="点击我,正好在我的下面显示元素B,且左边和我对齐">
</div>
<br><br><br><br><br><br><br><br>
<div class="tip">
我是元素B<br>
现在用的是position<br>
你会发现我现在的位置不正确
</div>
</body>
</html>

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

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

 <!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;width:960px;}
.parent{
border:3px solid #ccc;
width:600px;
height:300px;
margin-left:55px;
padding:25px;
}
input{
height:25px;
float:right;
}
.copyright{
position:absolute;
right:0;
}
.tip{
border:3px dotted #669900;
background:#eee;
width:200px;
position:absolute;
display:none;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$("input").bind("click",function(){
$(".tip").css({
left:$(this).position().left+"px",
top:$(this).position().top+25+"px"
});
$(".tip").toggle();
});
});
</script>
</head>
<body>
用position, 该例中元素B在button按钮的附近,也就是,元素B和button按钮有共同的父容器
<div class="parent" style="position:absolute;left:150px">
父元素是绝对定位的
<input type="button" value="点击我,正好在我的下面显示元素B,且左边和我对齐">
<div class="tip">
我是元素B<br>
现在用的是position,
我和按钮的父亲元素相同<br>
我能按要求正常显示
</div>
</div>
<br><br><br><br><br><br><br><br>
</body>
</html>

用offset五法正常显示的例子

 <!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;width:960px;}
.parent{
border:3px solid #ccc;
width:600px;
height:300px;
margin-left:55px;
padding:25px;
}
input{
height:25px;
float:right;
}
.copyright{
position:absolute;
right:0;
}
.tip{
border:3px dotted #669900;
background:#eee;
width:200px;
position:absolute;
display:none;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$("input").bind("click",function(){
$(".tip").css({
left:$(this).offset().left+"px",
top:$(this).offset().top+25+"px"
});
$(".tip").toggle();
});
});
</script>
</head>
<body>
用offset, 该例中元素B在button按钮的附近,也就是,元素B和button按钮有共同的父容器
<div class="parent" style="position:absolute;left:150px">
父元素是绝对定位的
<input type="button" value="点击我,正好在我的下面显示元素B,且左边和我对齐"> <div class="tip">
我是元素B<br>
现在用的是offset,
我和按钮的父亲元素相同
我无法按要求正常显示
</div>
</div>
<br><br><br><br><br><br><br><br>
</body>
</html>

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

【转载】http://www.jb51.net/article/18340.htm

Jquery中的offset()和position()深入剖析的更多相关文章

  1. Jquery中的offset()和position()深入剖析(元素定位)

    先看看这两个方法的定义. offset(): 获取匹配元素在当前视口的相对偏移. 返回的对象包含两个整形属性:top 和 left.此方法只对可见元素有效. position(): 获取匹配元素相对父 ...

  2. 关于jQuery中的offset()和position()

    在jQuery中有两个获取元素位置的方法offset()和position().position()方法是在1.2.6版本之后加入的,为什么要引 入这个方法呢?这两个方法之间有什么异同?使用的时候应该 ...

  3. 关于jQuery中的 offset() 和 position() 的用法

    ---恢复内容开始--- 在jQuery中有两个获取元素位置的方法offset()和position().position()方法是在1.2.6版本之后加入的,为什么要引入这个方法呢?这两个方法之间有 ...

  4. 区分jquery中的offset和position

    一次又一次地碰到需要获取元素位置的问题, 然后一次又一次地查offset和position的区别. 忍不了了, 这次一定得想办法记下来. position是元素相对于父元素的位置. 这个好记, par ...

  5. Jquery中的offset()和position()

    今天遇到这个偏移量的问题,特做此记录.以便日后查看. 先看看这两个方法的定义. offset(): 获取匹配元素在当前视口的相对偏移. 返回的对象包含两个整形属性:top 和 left.此方法只对可见 ...

  6. jquery 中的 offset()

    一.语法: 1.返回偏移坐标 $(select).offset(); top:  $(select).offset().top; left:  $(select).offset().left; 2.设 ...

  7. jquery中使用offset()获得的div的left=0,top=0

    写东西的时候要获取div的left和top,但怎么也取不到值都为0,但在chrome的console下是可以取到值的, 瞬间就纳闷了,于是乎就在网上找各种方法,大家一般的问题可能都是要获取的div被隐 ...

  8. js和JQuery中offset等属性对比

    HTML: 内容在滚动条下面 <div id="outerDiv"> <div id="myDiv" class="myDiv&qu ...

  9. 深度理解Jquery 中 offset() 方法

    参考原文:深度理解Jquery 中 offset() 方法

随机推荐

  1. keil5最新破解教程(可以使用到2032年哦!):

    keil5最新破解教程(可以使用到2032年哦!): 首先附上破解软件下载链接:https://github.com/lzfyh2017/keil5- 相信不少小伙伴使用的keil5都快要到期了,那么 ...

  2. 黑马IDEA版javaweb_2-2MySQL

    今日内容 数据库的基本概念 MySQL数据库软件 安装 卸载 配置 SQL 数据库的基本概念 1. 数据库的英文单词: DataBase 简称 : DB 2. 什么数据库? * 用于存储和管理数据的仓 ...

  3. PIL库参考文档之Image模块

    原文: https://pillow-cn.readthedocs.io/zh_CN/latest/reference/Image.html 中文版参考文档不全,所以自己试着翻译了一下,以下~备注部分 ...

  4. numpy(一)

    ndarray np的一个核心类,它描述了相同类型的“项目”集合.可以使用例如N个整数来索引项目.每个项目占用相同大小的内存块, 并且所有块都以完全相同的方式解释. 如何解释数组中的每个项目由单独的数 ...

  5. Codeforces 1288C - Two Arrays

    题目大意: 给定n和m,有两个数组,两个数组的长度都等于m 数组内每个元素都在1到n中 对于两个数组对应的位置i,必须满足a[i]<=b[i] a数组必须是不下降的序列 b数组必须是不上升的序列 ...

  6. springboot配置多个yml文件

    新接触了springboot项目,yml一大堆,启动不知道用的哪个,各种百度后: <profiles> <profile> <id>dev</id> & ...

  7. 刷题41. First Missing Positive

    一.题目说明 题目是41. First Missing Positive,求一个未排序队列中缺失的最小正整数.时间复杂度要求是O(n).难度是Hard,确实难. 二.我的解答 不考虑时间复杂度,首先对 ...

  8. CodeForces 996B World Cup(思维)

    https://codeforces.com/problemset/problem/996/B 题意: 圆形球场有n个门,Allen想要进去看比赛.Allen采取以下方案进入球场:开始Allen站在第 ...

  9. 一、Cookie和Session介绍

    会话跟踪 1. 什么是会话  * 用户拨打10086,从服务台接通后会话开始:  * 用户发出话费查询请求,服务台响应.这是该会话中的一个请求:  * 用户发出套餐变更请求,服务台响应.这是该会话中的 ...

  10. shell的集合运算

    用cat,sort,uniq命令实现文件行的交集 .并集.补集 交集 $F_1 \cap F_2 $ cat f1 f2 | sort | uniq -d 并集 $F_1 \cup F_2 $ cat ...