mouse posioin on ibject ????

B

Beshoo

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 ????
 
E

Elegie

Beshoo said:
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top