使用javascript的鼠标指针位置不适用于Internet Explorer

时间:2014-08-21 10:00:11

标签: javascript

尝试使用javascript获取鼠标指针位置时遇到问题。我的代码在文档Quirk模式下工作在ie10到ie7。但是当我更改ie浏览器版本时,默认情况下会在quirk ie5文档模式下更改它。在标准模式下,我的javascript无效。请帮助我。这是我的代码。我想用鼠标指针动态设置div宽度。

var IE = document.all?true:false




// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft;
    tempY = event.clientY + document.body.scrollTop;

    //alert("tempx"+tempX); 
    //alert("tempY"+tempY);
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX;
    tempY = e.pageY;

    //alert("tempx"+tempX); 

  }

  if(tempX <= 850){ 
   document.getElementById('content').style.width = tempX;
  }
  return true  

0 个答案:

没有答案