1.position():获取该元素距离有最近定位的父元素的位置
2:offset():获取该元素距离文档的位置
使用方法如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width:100px;
height: 100px;
margin-top: 100px;
}
.demo1{
position: relative;
background:green;
left: 100px;
}
.demo2{
position: absolute;
background: red;
left: 100px;
}
</style>
</head>
<body>
<div class = "demo1">
<div class = "demo2">
</div>
</div>
<script src="./jquery.js"></script>
<script>
var left1 = $('.demo2').position().left;
var left2 = $('.demo2').offset().left;
console.log(left1);
console.log(left2);
</script>
</body>
</html>
运行结果: