对于javascript中各种位置属性,之前在写特效时是需要什么就用什么,还没有认认真真的系统的去总结下。在很多特效中位置属性起着举足轻重的作用。如果浏览器对每种属性的解释都相同,我们也不用浪费太多的精力,因为对于每一种属性,不同的浏览器有着不同的解释。
这篇文章涵盖了目前各种主流对象的各种位置的属性,如果有什么错误的地方或者遗漏的地方请不要吝惜你的文字。
总结这些属性,也算是对自己有个交代。
window对象
- innerHeight
- innerWidth
- outerHtight
- outerWidth
- pageXOffset
- pageXOffset
- screenLeft
- screenTop
- screenX
- ScreenY
innerHeight innerWidth
innerHeight |
返回窗口的文档显示区的高度 |
innerWidth |
返回窗口的文档县市区的宽度 |
浏览器兼容性:
实例代码:【注意在FF中测试】
<html>
<head>
<script type="text/javascript">
function openWin()
{
myWindow=window.open('','','top=0,left=0');
myWindow.innerHeight="200";
myWindow.innerWidth="200";
myWindow.document.write("<p>This is 'myWindow'</p>");
myWindow.focus();
}
</script>
</head>
<body>
<input type="button" value="Open 'myWindow'" onclick="openWin()" />
</body>
</html>
效果实现:
outerHeight outerWidth
outerHeight |
返回窗口的外部高度,包括工具栏和滚动条 |
outerWidth |
返回窗口的外部宽度,包括工具栏和滚动条 |
浏览器兼容性:
代码实现:【在FF中测试】
<html>
<head>
<script type="text/javascript">
function openWin()
{
myWindow=window.open('','');
myWindow.outerHeight="200";
myWindow.outerWidth="200";
myWindow.document.write("<p>This is 'myWindow'</p>");
myWindow.focus();
}
</script>
</head>
<body>
<input type="button" value="Open 'myWindow'" onclick="openWin()" />
</body>
</html>
效果实现:
pageXOffset pageYOffset
pageXOffset |
声明当前文档向右滚动过的像素数 |
pageYOffset |
声明当前文档向下滚动过的像素数 |
IE不支持这些属性,IE使用document.dacumentElement或document.body的scrollLeft和scrollTop属性。
浏览器兼容性:
实例代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
</head>
<body>
<script type="text/javascript">
function set(){
window.scrollBy(100,100);
alert("pageXOffset: " + window.pageXOffset + ", pageYOffset: " + window.pageYOffset);
return true;
}
</script>
<input type="button" value="click" onclick="set()"/>
1
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
1111111111111111111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
2
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
</body>
</html>
效果实例:
screenTop screenLeft screenX screenY
screenTop |
只读整数,声明窗口的左上角的Y坐标 |
screenLeft |
只读整数,声明窗口的左上角的X坐标 |
screenX |
只读整数,声明窗口的左上角的X坐标 |
screenY |
只读整数,声明窗口的左上角的Y坐标 |
浏览器兼容性:
screenTop screenLeft
screenX screenY
实例代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
</head>
<body>
<script type="text/javascript">
function test(){
if(window.screenTop){
alert(screenLeft+"::"+screenTop);
}else if(window.screenX){
alert(screenX+"::"+screenY);
}else alert("this is a joke");
}
</script>
<input type="button" value="click" onclick="test()"/>
</body>
</html>
效果实例:
在Chrom中测试:
在FF中测试: