Getting Mouse Position

Discussion in 'Javascript' started by Andy Levy, Oct 17, 2003.

  1. Andy Levy

    Andy Levy Guest

    Hi

    I am using the following script to get the mouse x and y position values.
    It seems to work in all browsers ok, except for IE on the Mac. Both
    xMousePos and yMousePos do not seem to accurately relate to where the mouse
    is on the screen. But only in IE on the Mac. Any ideas ?

    Thanks
    Andy

    <!--
    if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
    } else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
    } else if (document.getElementById) { // Netcsape 6
    document.onmousemove = captureMousePosition;
    }

    // Global variables
    xMousePos = 0; // Horizontal position of the mouse on the screen
    yMousePos = 0; // Vertical position of the mouse on the screen
    xMousePosMax = 0; // Width of the page
    yMousePosMax = 0; // Height of the page

    function captureMousePosition(e) {

    if (document.layers) { //Netscape
    xMousePos = e.pageX;
    yMousePos = e.pageY;
    xMousePosMax = window.innerWidth+window.pageXOffset;
    yMousePosMax = window.innerHeight+window.pageYOffset;
    } else if (document.all) { //IE
    xMousePos = window.event.x+document.body.scrollLeft;
    yMousePos = window.event.y+document.body.scrollTop;
    xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
    yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (document.getElementById) {

    // Netscape 6 behaves the same as Netscape 4 in this regard
    xMousePos = e.pageX;
    yMousePos = e.pageY;
    xMousePosMax = window.innerWidth+window.pageXOffset;
    yMousePosMax = window.innerHeight+window.pageYOffset;
    }

    //window.status = "xMousePos=" + xMousePos + ", yMousePos=" + yMousePos +
    ", xMousePosMax=" + xMousePosMax + ", yMousePosMax=" + yMousePosMax;


    }
    Andy Levy, Oct 17, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Davisro
    Replies:
    2
    Views:
    1,292
    Jim Corey
    Apr 23, 2004
  2. Max
    Replies:
    7
    Views:
    9,087
  3. tom arnall
    Replies:
    6
    Views:
    1,263
    Ian Shef
    Jan 18, 2007
  4. Weston C
    Replies:
    1
    Views:
    76
    john henry bonham
    Sep 17, 2004
  5. James Black
    Replies:
    0
    Views:
    393
    James Black
    May 28, 2006
Loading...

Share This Page