Getting Mouse coordinates in a DIV

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonne

    #1

    Getting Mouse coordinates in a DIV

    Hi,
    I haven't found anything like this anywhere with Google, so I'm posting
    it here, hoping one of you people knows how to do something like this.
    I'm trying to get the mouse coordinates in a div, as opposed to relative
    to the screen.

    _______________ _______________ _
    | * |
    |______________ _______________ __|

    Say this DIV is 100px wide, and the mouse is located at 60px from the
    left side of the DIV, how can I get 60px as a value from that? It should
    work in Firefox and IE (and preferably Opera, Konqueror, Safari, etc too
    ;) )

    Is the only way to get mouse coordinates this:

    function getMouseXY(e) {
    if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.s crollLeft;
    tempY = event.clientY + document.body.s crollTop;
    }
    else { // grab the x-y pos.s if browser is NS
    tempX = e.pageX;
    tempY = e.pageY;
    }
    ?

    Or can I use DOM to grab it through getElementByID ?

    thanks in advance
    Jonne
  • Fred Oz

    #2
    Re: Getting Mouse coordinates in a DIV

    Jonne wrote:
    [...][color=blue]
    > Is the only way to get mouse coordinates this:
    >
    > function getMouseXY(e) {
    > if (IE) { // grab the x-y pos.s if browser is IE
    > tempX = event.clientX + document.body.s crollLeft;
    > tempY = event.clientY + document.body.s crollTop;
    > }
    > else { // grab the x-y pos.s if browser is NS
    > tempX = e.pageX;
    > tempY = e.pageY;
    > }
    > ?[/color]

    You should make your event capture routine more cross-browser and
    swap feature detection for browser detection:

    function getMouseXY(e) {
    var e = e || window.event;

    // grab the x-y pos.s if browser supports clientX/Y
    if ( e.clientX && document.body.s crollLeft
    && document.body.s crollTop ) {
    tempX = e.clientX + document.body.s crollLeft;
    tempY = e.clientY + document.body.s crollTop;

    // Otherwise, try pageX/Y
    } else if (e.pageX) {
    ...
    }

    I imagine you also need to get the postion of the element too,
    for that document.getEle mentById may be useful to get a reference
    to the div.

    Have a read here for a pointer to the issues with trying to get
    the cursor position:

    <URL:http://www.quirksmode. org/js/events_properti es.html>


    --
    Fred

    Comment

    • Stephen Chalmers

      #3
      Re: Getting Mouse coordinates in a DIV

      Fred Oz <ozfred@iinet.n et.auau> wrote in message
      news:421f28e6$0 $30573$5a62ac22 @per-qv1-newsreader-01.iinet.net.au ...
      [color=blue]
      > if ( e.clientX && document.body.s crollLeft
      > && document.body.s crollTop )[/color]

      These properties are numeric, so even if they are all defined, the test
      evaluates false unless they are all non-zero.

      --
      S.C.


      Comment

      • Fred Oz

        #4
        Re: Getting Mouse coordinates in a DIV

        Stephen Chalmers wrote:[color=blue]
        > Fred Oz <ozfred@iinet.n et.auau> wrote in message
        > news:421f28e6$0 $30573$5a62ac22 @per-qv1-newsreader-01.iinet.net.au ...
        >
        >[color=green]
        >> if ( e.clientX && document.body.s crollLeft
        >> && document.body.s crollTop )[/color]
        >
        >
        > These properties are numeric, so even if they are all defined, the test
        > evaluates false unless they are all non-zero.
        >[/color]

        Yes, that is so. Detection of such features is difficult, as not
        only are they browser dependent, but also may be unavailable
        based on doctype:

        <URL:http://www.quirksmode. org/viewport/compatibility.h tml>

        I guess testing for them needs to be separated from the clientX/Y
        test and put inside try/catch: if they return zero, then the
        scroll offset it zero. If they error, then the offset is unknown
        and the OP must take a guess at what to do.


        --
        Fred

        Comment

        • RobB

          #5
          Re: Getting Mouse coordinates in a DIV

          Fred Oz wrote:[color=blue]
          > Stephen Chalmers wrote:[color=green]
          > > Fred Oz <ozfred@iinet.n et.auau> wrote in message
          > >[/color][/color]
          news:421f28e6$0 $30573$5a62ac22 @per-qv1-newsreader-01.iinet.net.au ...[color=blue][color=green]
          > >
          > >[color=darkred]
          > >> if ( e.clientX && document.body.s crollLeft
          > >> && document.body.s crollTop )[/color]
          > >
          > >
          > > These properties are numeric, so even if they are all defined, the[/color][/color]
          test[color=blue][color=green]
          > > evaluates false unless they are all non-zero.
          > >[/color]
          >
          > Yes, that is so. Detection of such features is difficult, as not
          > only are they browser dependent, but also may be unavailable
          > based on doctype:
          >
          > <URL:http://www.quirksmode. org/viewport/compatibility.h tml>
          >
          > I guess testing for them needs to be separated from the clientX/Y
          > test and put inside try/catch: if they return zero, then the
          > scroll offset it zero. If they error, then the offset is unknown
          > and the OP must take a guess at what to do.
          >
          >
          > --
          > Fred[/color]

          Why not...

          function getMouseXY(e)
          {
          e = e || window.event;
          if (e)
          {
          // grab the x-y pos.s if browser supports clientX/Y
          if (document.body
          && typeof document.body.s crollLeft != 'undefined'
          && typeof e.clientX != 'undefined')
          {
          var tempX = e.clientX + document.body.s crollLeft;
          var tempY = e.clientY + document.body.s crollTop;
          }
          // Otherwise, try pageX/Y
          else if (typeof e.pageX != 'undefined')
          {...

          Comment

          Working...