forked from SimplyWenjing/SimplyWenjing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
size.html
66 lines (57 loc) · 2.04 KB
/
size.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>
<head>
<meta charset="utf-8">
<title>精准定位元素的参数</title>
<style type="text/css">
#element {
height: 600px;
overflow: scroll;
width: 500px;
border:8px solid red;
margin: 30px auto;
padding: 0;
}
#son-element{
background: green;
border:8px solid #eee;
width: 400px;
height: 800px;
margin: 30px auto;
}
</style>
</head>
<body>
<div id="element">
Hello element
<div id="son-element"></div>
</div>
<script type="text/javascript">
var element = document.getElementById("element");
var son = element.firstElementChild;
var elementScrollH = element.scrollHeight;//整个元素的大小,包括边框,如果有滚动条就是去掉滚动条后,整个元素撑开的大小
var elementClientH = element.clientHeight;// 元素的大小,不考虑滚动,不包括边框,=内容+padding
var elementOffsetH = element.offsetHeight;// 元素大小,包括边框,不考虑滚动
var elementOffsetW = element.offsetWidth;
var elementOffsetT = element.offsetTop; //因为有滚动,所以有的内容被隐藏,这里指被隐藏起来的部分的高度
//console.log(son.height);
//console.log("scrollHeight:" + sonScrollH +"//////" + "clientHeight:" + sonClientH);
son.addEventListener("click",function(event){
alert("clientX:" + event.clientX);// clientX 是到浏览器显示区域的偏移量,不包括滚动条和工具栏
alert("screenX:" + event.screenX)//screenX是到屏幕的偏移量
alert("offsentX:" + event.offsetX); //到元素内边框的
},false);
function scrollToTop (element){
if(element.scrollTop != 0){
element.scrollTop = 0;
}
}
element.addEventListener("click", function(){
scrollToTop(element);
}, false);
element.addEventListener("click",function (event) {
alert("offsetHeight:" + elementOffsetH + "; scrollHeight:" + elementScrollH + "; clientHeight:" + elementClientH + "; offsetWidth:" + elementOffsetW + "; offsetTop:" + elementOffsetT);
}, false );
</script>
</body>
</html>