Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if mouse is over an element when page loads with Javascript

Tags:

javascript

I have an image that I want to have trigger certain behaviors when the mouse is over, I have a mouseover and mouseout method, but if you happen to have your mouse over the image when the page loads, the mouseover method never fires until you leave the image and come back over it.

Is there a way to detect if the mouse is over an element on the fly without the mouse having to be off of the element and then come over the element to trigger the JS mouseover event? Like is there a document.getElementById("blah").mouseIsOver() type function in Javascript?

like image 334
cmcculloh Avatar asked Oct 05 '09 17:10

cmcculloh


1 Answers

I believe this is possible without any action from the user. When your page loads, bind the mouseover event to your image and hide your image (i.e. using CSS display:none). Use setTimeout() to show it again in a few milliseconds (10 should be enough). The even should be fired.

If you don't want to cause the 'flick' effect on your image, you may try using some temporary element instead, attaching event to it, and delegating the event onto your image.

I have no idea if this is cross-browser solution, but it worked from my Firefox 3.0 console ;)

like image 175
wildcard Avatar answered Sep 30 '22 16:09

wildcard