@wajiw posted a great solution, but unfortunately it suffers from typos, which means it wonβt work out of the box until you fix it.
Here is a class that you can use that is tested and working, which allows you to check if the object is under the mouse.
Class definition
// keeps track of recent mouse position and provides functionality to check if mouse is over an object // useful for when nodes appear underneath the mouse without mouse movement and we need to trigger hover // see http://stackoverflow.com/questions/4403518 function MouseTracker($) { var mouseX, mouseY; $(document).mousemove(function(e) { mouseX = e.pageX; mouseY = e.pageY; }); return { isOver: function(node) { var p = $(node).offset(); if (mouseX >= p.left && mouseX <= p.left + $(node).width() && mouseY >= p.top && mouseY <= p.top + $(node).height()) { return true; } return false; } } }
Usage example
var mouseTracker = new MouseTracker(jQuery); if (mouseTracker.isOver($('#my-object-in-question'))) { $('#my-object-in-question').trigger("mouseenter"); }
Hope this helps.
I could make it into the jQuery plugin very easily, if anyone wants to, just email me and I will go ahead.
Matt
Matthew O'Riordan
source share