-
Notifications
You must be signed in to change notification settings - Fork 0
/
07-offset方法与position方法.html
66 lines (54 loc) · 1.24 KB
/
07-offset方法与position方法.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
margin: 0;
padding: 0;
}
.father {
width: 400px;
height: 400px;
background-color: pink;
margin: 100px;
position: relative;
}
.son {
width: 300px;
height: 300px;
background-color: red;
margin-left: 100px;
}
.sun {
width: 200px;
height: 200px;
background-color: goldenrod;
position: absolute;
left: 50px;
top: 50px;
}
</style>
</head>
<body>
<div class="father">
<div class="son">
<div class="sun"></div>
</div>
</div>
<script src="jquery-1.12.4.js"></script>
<script>
$(function () {
//offset: 获取的是直接相对与body的位置, 返回的也是个对象,left top
//console.log($(".sun").offset().left);
//$(".sun").offset({left:500, top:500});
//postion: 获取的是相对于有定位的父元素的位置,返回的是对象,left和top
//console.log($(".sun").position().left);
//console.log($(".sun").position().top);
//postion只能获取,不能设置。
//$(".sun").css({left:100, top:100});
});
</script>
</body>
</html>