obj.offset[Width|Height|Top|Left]  取控件相对于父控的位置
event.offset[X|Y] 取鼠标相在触发事件的控件中的坐标
event.screen[X|Y] 鼠标相对于屏幕坐标
event.client[X|Y]   鼠标相对于网页坐标在在
obj.scroll[Width|Height|Top|Left] 获取对象滚动的大小
obj.client[Width|Height|Top|Left] 获取对象可见区域的大小

以下为测试代码
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ />
<title>无标题文档</title>
<style type=”text/css”>
<!–
div{
font-family: “宋体”;
font-size: 12px;
color: #000000;
}
#div1{
position:absolute;
background-color:#f0f0f0;
width:200px;
height:200px;
left:20px;
top:0px;
z-index:1;
}
#div2{
background-color:#cfc0cf;
width:200px;
height:210px;
position:absolute;
left:261px;
top:347px;
z-index:100;
}
#div3{
background-color:#abbfbf;
width:200px;
height:200px;
position:absolute;
left:20px;
top:247px;
z-index:100;
}
#div4{
background-color:#cfcfcf;
width:200px;
height:200px;
position:absolute;
left:461px;
top:147px;
z-index:100;
}
–>
</style>

 

</head>

<body>
<div id=’div1′ onclick=’eventc(this)’></div>
<div id=’div2′ onclick=’client(this);’></div>
<div id=’div3′ onclick=’screen(this);’></div>
<div id=’div4′onclick=’offset(this);’>offset 控件相对于父窗体的位置</div>
<script>
function offset(ids){
ids.innerHTML=”offsetLeft =”+ids.offsetLeft+”<BR>”;
ids.innerHTML+=”offsetWidth =”+ids.offsetWidth+”<BR>”;
ids.innerHTML+=”offsetHeight =”+ids.offsetHeight+”<BR>”;
ids.innerHTML+=”offsetTop =”+ids.offsetTop+”<BR>”;
ids.innerHTML+=”event.offset 鼠标相对于控件的位置<BR>”;
ids.innerHTML+=”offsetX =”+event.offsetX+”<BR>”;
ids.innerHTML+=”offsetY =”+event.offsetY+”<BR>”;
}
function screen(ids){
ids.innerHTML=”scrollWidth =”+ids.scrollWidth+”<BR>”;
ids.innerHTML+=”scrollHeight =”+ids.scrollHeight+”<BR>”;
ids.innerHTML+=”scrollTop =”+ids.scrollTop+”<BR>”;
ids.innerHTML+=”scrollLeft =”+ids.scrollLeft+”<BR>”;
}
function client(ids){
ids.innerHTML=”clientWidth =”+ids.clientWidth+”<BR>”;
ids.innerHTML+=”clientHeight =”+ids.clientHeight+”<BR>”;
ids.innerHTML+=”clientTop =”+ids.clientTop+”<BR>”;
ids.innerHTML+=”clientLeft =”+ids.clientLeft+”<BR>”;
}
function eventc(ids){
ids.innerHTML=”鼠标相对于屏幕坐标<BR>event.screenX=”+event.screenX+”<BR>”;
ids.innerHTML+=”event.screenY =”+event.screenY+”<BR>”;
ids.innerHTML+=”鼠标相对于网页坐标event.clientX=”+event.clientX+”<BR>”;
ids.innerHTML+=”event.clientY =”+event.clientY+”<BR>”;
}
</script>
</body>
</html>

一个鼠标拖动的小例子
<html>
<head>
<style>
#st1{
position:absolute;
left:161;
top:147;
width:309;
height:262;
z-index:1;
background-color:#f0f0f0;
}
</style>
</head>
<body>
<div id=’st1′ style=”wdith:100px” onmouseup=’moveDiv.show();’ onmousedown=’moveDiv.preeDown()’
onmousemove=’moveDiv.mouseMove()’ onmouseout=’moveDiv.show()’>
</div>
</body>
</html>
<script>
var moveDiv = new function(){
var status = 0;
var left = 161;
var top = 147;
var ids = document.getElementById(‘st1′);
var mx = 0;
var my = 0;
this.preeDown = function(){
status=1;
mx = event.offsetX;
my = event.offsetY;
}

this.mouseMove = function(){
if(status==1){
ids.style.left=event.clientX+document.body.scrollLeft-mx;
ids.style.top=event.clientY+document.body.scrollTop-my;

}
}

this.show=function(){
status=0;
}
}
</script>

转自:http://hi.baidu.com/tianlong1569/blog/item/91e2a1d345ff843c960a1681.html

© 2012 Hedgehog Suffusion theme by Sayontan Sinha