JavaScript: test if a div is hovered
Solved
electroking
Posted messages
276
Status
Member
-
electroking Posted messages 276 Status Member -
electroking Posted messages 276 Status Member -
Hi everyone, so I have a div
and I’d like to know how a JavaScript function with a setInterval
could check if madiv is hovered by the cursor.
Thanks in advance for your answer.
Signature removed by moderation.
<div id="madiv">#contenu</div>
and I’d like to know how a JavaScript function with a setInterval
window.setInterval("mafonction()", 1); function mafonction() { if ( /* if madiv is hovered */ ){ /* XXX */ } } could check if madiv is hovered by the cursor.
Thanks in advance for your answer.
Signature removed by moderation.
2 answers
-
Hello,
You can get something by using onmouseover/mouseenter with an event handler/listener, see here
https://developer.mozilla.org/en-US/docs/Web/Events/mouseover
or you use the function fallback or directly the event object (compare its value)
Or a cheating trick using the mouse coordinates to detect if it is in the right area. -
Hi,
If you’re allowed to include jQuery on your page, there’s an easy solution:
https://api.jquery.com/mouseenter/
$( "#madiv" ).mouseenter(function() { /* XXX */ });