mouse posioin on ibject ????

Discussion in 'Javascript' started by Beshoo, Apr 20, 2007.

  1. Beshoo

    Beshoo Guest

    Do u have any idea how can i detect if the mouse on acertain object
    for exapmle I have <div> i wnat if the mouse on this <div> take an
    action ????
    Beshoo, Apr 20, 2007
    #1
    1. Advertising

  2. Beshoo

    -Lost Guest

    -Lost, Apr 20, 2007
    #2
    1. Advertising

  3. Beshoo

    Elegie Guest

    Beshoo wrote:

    > Do u have any idea how can i detect if the mouse on acertain object
    > for exapmle I have <div> i wnat if the mouse on this <div> take an
    > action ????


    There are many events that you can monitor to do that (check the link
    offered by Lost). Many approaches are possible: you can watch the
    mousemove event, which is fired each time the mouse moves over on
    element (but not when the mouse does not move anymore), or watch the
    mouseover and mouseout events, to define some mouse state for your
    element (beware of event bubbling). See below.

    ---
    <head>
    <style type="text/css">
    div {background-color:yellow; margin:10px}
    </style>
    </head>
    <body ondblclick="alert('Is the mouse over foo: ' + m())">
    <div id="info">Double click anywhere.</div>
    <div id="foo">I am foo!</div>
    <div id="not-foo">I am not foo!</div>
    <script type="text/javascript">
    // --- get some monitor function ---//
    var m=monitorMouseMoveForElement(
    document.getElementById("foo")
    );

    // --- lib ---//
    function monitorMouseMoveForElement(elem) {
    var isMouseOver = false;

    _e(elem, "onmouseover",function (evt) { isMouseOver=true; });
    _e(elem, "onmouseout",
    function (evt) {
    isMouseOver = contains(
    this, (evt=evt||window.event, evt.relatedTarget||evt.toElement)
    );
    }
    );

    function contains(cer, cee){
    if(cer.contains) {
    return cer.contains(cee);
    } else if(typeof cee.parentNode!= "undefined") {
    return (function(cr, ce){
    return cr==ce ||
    ce&&ce.parentNode&&arguments.callee(cr,ce.parentNode)||
    false ;
    })(cer, cee);
    } else {
    return false;
    }
    }

    function _e(obj, evt, func) {
    if(obj[evt]) {
    obj[evt] = (function(x){
    return function (evt) {
    func.call(this, evt);
    return x.call(this, evt);
    }
    })(obj[evt]);
    } else {
    obj[evt] = func ;
    }
    }

    return function() {
    return isMouseOver;
    };
    }
    </script>
    </body>
    ---


    HTH,
    Elegie.
    Elegie, Apr 20, 2007
    #3
  4. Beshoo

    Guest

    , Apr 21, 2007
    #4
    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,306
    Jim Corey
    Apr 23, 2004
  2. Max
    Replies:
    7
    Views:
    9,098
  3. Claus Atzenbeck
    Replies:
    1
    Views:
    1,080
    Claus Atzenbeck
    Nov 26, 2004
  4. =?Utf-8?B?am9qb2Jhcg==?=
    Replies:
    1
    Views:
    4,082
    wwwtar
    Oct 2, 2006
  5. tom arnall
    Replies:
    6
    Views:
    1,275
    Ian Shef
    Jan 18, 2007
Loading...

Share This Page