JavaScript得到鼠标指针相对于浏览器页面(或客户区)的水平坐标(兼容ie,ff)

1.客户区:指的是当前窗口

2.如果页面使用母版页得到的X,Y轴坐标和不使用母版页得到的X,Y轴坐标不相同

 

var hovertipMouseX;

var hovertipMouseY;

function hovertipMouseUpdate(e) 
{

  
var mouse = hovertipMouseXY(e);

  hovertipMouseX 
= mouse[0];

  hovertipMouseY 
= mouse[1];

}

function hovertipMouseXY(e)
{

  
if!e ) 
  
{
    
if( window.event ) 
    
{
      
//Internet Explorer
      e = window.event;
    }
 
   
else 
    
{
      
//total failure, we have no way of referencing the event
      return;
    }

  }

  
iftypeof( e.pageX ) == 'number' )
  
{
    
//most browsers
    var xcoord = e.pageX;
    
var ycoord = e.pageY;
   }
 
  
else iftypeof( e.clientX ) == 'number' ) 
  
{
    
//Internet Explorer and older browsers
    //other browsers provide this, but follow the pageX/Y branch
    var xcoord = e.clientX;
    
var ycoord = e.clientY;
  }

  
var badOldBrowser = ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) ||
   ( window.ScriptEngine 
&& ScriptEngine().indexOf( 'InScript' ) + 1 ) ||
   ( navigator.vendor 
== 'KDE' );
   
if!badOldBrowser ) 
   
{
     
if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
       
{
       
//IE 4, 5 & 6 (in non-standards compliant mode)
       xcoord += document.body.scrollLeft;
       ycoord 
+= document.body.scrollTop;
      }

    
else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
     
{
       
//IE 6 (in standards compliant mode)
        xcoord += document.documentElement.scrollLeft;
        ycoord 
+= document.documentElement.scrollTop;
     }

  }

  
else 
  
{
   
//total failure, we have no way of obtaining the mouse coordinates
   return;
  }

    
return [xcoord, ycoord];
  }
posted @ 2008-07-28 11:28  MicroCoder  阅读(765)  评论(0编辑  收藏  举报