详解 javascript中offsetleft属性的用法
转载 2015-11-11 投稿:mrr 我要评论
此属性可以返回当前元素距离某个父辈元素左边缘的距离,当然这个父辈元素也是有讲究的。
(1).如果父辈元素中有定位的元素,那么就返回距离当前元素最近的定位元素边缘的距离。
(2).如果父辈元素中没有定位元素,那么就返回相对于body左边缘距离。
语法结构:
obj.offsetleft
特别说明:此属性是只读的,不能够赋值。
代码实例:
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
|
<!DOCTYPE html>
<html>
<head>
<meta charset=
"utf-8"
>
<title>蚂蚁部落</title>
<style type=
"text/css"
>
*{
margin: 0px;
padding: 0px;
}
#main{
width:300px;
height:300px;
background:red;
position:absolute;
left:100px;
top:100px;
}
#box{
width:200px;
height:200px;
background:blue;
margin:50px;
overflow:hidden;
}
#inner{
width:50px;
height:50px;
background:green;
text-align:center;
line-height:50px;
margin:50px;
}
</style>
<script type=
"text/javascript"
>
window.onload=
function
(){
var
inner=document.getElementById(
"inner"
);
inner.innerHTML=inner.offsetLeft;
}
</script>
</head>
<body>
<div id=
"main"
>
<div id=
"box"
>
<div id=
"inner"
></div>
</div>
</div>
</body>
</html>
|
上面的代码可以返回inner元素距离main元素的左侧的距离,因为main元素是第一个定位父辈元素。
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
|
<!DOCTYPE html>
<html>
<head>
<meta charset=
"utf-8"
>
<title>蚂蚁部落</title>
<style type=
"text/css"
>
*{
margin: 0px;
padding: 0px;
}
#main{
width:300px;
height:300px;
background:red;
margin:100px;
}
#box{
width:200px;
height:200px;
background:blue;
overflow:hidden;
}
#inner{
width:50px;
height:50px;
background:green;
text-align:center;
line-height:50px;
margin:50px;
}
</style>
<script type=
"text/javascript"
>
window.onload=
function
(){
var
inner=document.getElementById(
"inner"
);
inner.innerHTML=inner.offsetLeft;
}
</script>
</head>
<body>
<div id=
"main"
>
<div id=
"box"
>
<div id=
"inner"
></div>
</div>
</div>
</body>
</html>
|
上面的代码返回inner元素距离body元素左侧的尺寸。
此属性具有一定的兼容性问题,具体可以参阅offsetleft兼容性简单介绍一章节。
ps:js中的offsetLeft属性具体有什么作用?
可以判断一个物体的跟document的左边距离,也就是浏览器左边缘。比如你写一个div 获取这个div之后alert(你的div.offsetLeft)就可以看到他现在距离浏览器左边的距离。当然你也可以用他给对象赋值,offset不单单只有Left 还有offsetTop offsetWidth offsetHeight 都是JS里很有用的属性。
谈谈对offsetleft兼容性的理解
转载 2015-11-11 投稿:mrr 我要评论
关于此属性的基本用法可以参阅offsetleft属性用法详解一章节。
此属性具有一定的兼容性问题,那就是在IE7浏览器中,它的返回值是想对于最近的父辈元素的左侧的距离。
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
|
<!DOCTYPE html>
<html>
<head>
<meta charset=
"utf-8"
>
<title>蚂蚁部落</title>
<style type=
"text/css"
>
* {
margin: 0px;
padding: 0px;
}
#main {
width: 300px;
height: 300px;
background: red;
position: absolute;
left: 100px;
top: 100px;
}
#box {
width: 200px;
height: 200px;
background: blue;
margin:50px;
overflow:hidden;
}
#inner {
width: 50px;
height: 50px;
background: green;
text-align: center;
line-height: 50px;
margin: 50px;
}
</style>
<script type=
"text/javascript"
>
window.onload=
function
(){
var
inner=document.getElementById(
"inner"
);
inner.innerHTML=inner.offsetLeft;
}
</script>
</head>
<body>
<div id=
"main"
>
<div id=
"box"
>
<div id=
"inner"
></div>
</div>
</div>
</body>
</html>
|
上面的代码在其他浏览器中返回值是100,但是在IE7浏览器中返回值是50。
至于IE6没有测试,感兴趣的大家可以做一下测试。
下面抽点空给大家介绍offsetLeft与style.left的区别
offsetLeft 获取的是相对于父对象的左边距
left 获取或设置相对于 具有定位属性(position定义为relative)的父对象 的左边距
如果父div的position定义为relative,子div的position定义为absolute,那么子div的style.left的值是相对于父div的值,
这同offsetLeft是相同的,区别在于:
1. style.left 返回的是字符串,如28px,offsetLeft返回的是数值28,如果需要对取得的值进行计算,
还用offsetLeft比较方便。
2. style.left是读写的,offsetLeft是只读的,所以要改变div的位置,只能修改style.left。
3. style.left的值需要事先定义,否则取到的值为空。而且必须要定义在html里,我做过试验,如果定义在
css里,style.left的值仍然 为空,这就是我刚开始碰到的问题,总是取不到style.left的值。
offsetLeft则仍然能够取到,无需事先定义div的位置。