scrolltop + clientY无法在Mozilla Firefox中运行

时间:2011-06-11 09:30:36

标签: c# javascript asp.net

var YPos12 = event.clientY + document.body.scrollTop;

不适用于Mozilla Firefox。任何人都可以给我代码的工作示例?请帮忙

1 个答案:

答案 0 :(得分:0)

如果您无法使用以下内容,我建议您开始使用jQuery或其他javscript框架来提供帮助。我个人使用jquery,但看看那里可用的javascript frameworks,它们让生活更轻松。

完全替换为完整的工作示例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<head>
<title>Mouse Co-ordinates Test</title>
<script type="text/javascript">
var posX;
var posY;

function mouseMovement(newEvent){
  posX=0;posY=0;
  var myEvent=(!newEvent)?window.event:newEvent;//IE:Moz
  if (myEvent.pageX){//Moz
    posX=myEvent.pageX+window.pageXOffset;
    posY=myEvent.pageY+window.pageYOffset;
  }
  else if(myEvent.clientX){//IE
    posX=myEvent.clientX+document.body.scrollLeft;
    posY=myEvent.clientY+document.body.scrollTop;
  }
  else {
    return false
  }//old browsers

  // Your X & Y values will be available here via  posX & posY
  document.getElementById('movementspan').firstChild.data='X='+posX+' Y='+posY;
}

function myClickEvent(myClick) {
  document.getElementById('clickspan').firstChild.data='X='+posX+' Y='+posY;
}
document.onmousemove=mouseMovement;
document.onclick=myClickEvent;
</script>

</head>
<body onload="init()">
Mouse Co-ordinates 
<span id="movementspan">&nbsp;</span>
<br />
Mouse co-ordinates at click 
<span id="clickspan">&nbsp;</span>

</body>
</html>