在页面上拖拽层首先要做的就是定位当前鼠标的位置,看看下面代码javascript定位鼠标。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> 定位鼠标 </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="sylar">
<META NAME="Keywords" CONTENT="javascript">
<META NAME="Description" CONTENT="">
</HEAD>
<script>
function getMousePosition()
{
var e = e||window.event;
var objSrc = e.srcElement;//event source element
var left = 0;
var top = 0;
while (objSrc.offsetParent){
left += objSrc.offsetLeft;
top += objSrc.offsetTop;
objSrc = objSrc.offsetParent;
}
left += objSrc.offsetLeft;//source element's offsetTop
top += objSrc.offsetTop;//source element's offsetLeft
//alert(e.offsetX);
mouseOffsetX = left + e.offsetX;
mouseOffsetY = top + e.offsetY;
mX.value = mouseOffsetX;
mY.value = mouseOffsetY;
}
document.onmousemove = getMousePosition;
</script>
<BODY>
鼠标X坐标:<input type=text name=mX size=10>
鼠标Y坐标:<input type=text name=mY size=10>
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE> 定位鼠标 </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="sylar">
<META NAME="Keywords" CONTENT="javascript">
<META NAME="Description" CONTENT="">
</HEAD>
<script>
function getMousePosition()
{
var e = e||window.event;
var objSrc = e.srcElement;//event source element
var left = 0;
var top = 0;
while (objSrc.offsetParent){
left += objSrc.offsetLeft;
top += objSrc.offsetTop;
objSrc = objSrc.offsetParent;
}
left += objSrc.offsetLeft;//source element's offsetTop
top += objSrc.offsetTop;//source element's offsetLeft
//alert(e.offsetX);
mouseOffsetX = left + e.offsetX;
mouseOffsetY = top + e.offsetY;
mX.value = mouseOffsetX;
mY.value = mouseOffsetY;
}
document.onmousemove = getMousePosition;
</script>
<BODY>
鼠标X坐标:<input type=text name=mX size=10>
鼠标Y坐标:<input type=text name=mY size=10>
</BODY>
</HTML>