Is there a quick & easy way to do this in jQuery that I'm missing?
I don't want to use the mouseover event because I'm already using it for something else. I just need to know if the mouse is over an element at a given moment.
I'd like to do something like this, if only there was an "IsMouseOver" function:
function hideTip(oi) {
setTimeout(function() { if (!IsMouseOver(oi)) $(oi).fadeOut(); }, 100);
}
In jQuery you can use .is(':hover'), so
would now be the most concise way to provide the function requested in the OP.
Note: The above does not work in IE8 or lower
As less succinct alternative that does work in IE8 (if I can trust IE9's IE8 modus), and does so without triggering
$(...).hover(...)
all over the place, nor requires knowing a selector for the element (in which case Ivo's answer is easier):This code illustrates what happytime harry and I are trying to say. When the mouse enters, a tooltip comes out, when the mouse leaves it sets a delay for it to disappear. If the mouse enters the same element before the delay is triggered, then we destroy the trigger before it goes off using the data we stored before.
Here's a technique which doesn't rely on jquery and uses the native DOM
matches
API. It uses vendor prefixes to support browsers going back to IE9. See matchesselector on caniuse.com for full details.First create the matchesSelector function, like so:
Then, to detect hover:
You can use jQuery's
hover
event to keep track manually:I needed something exactly as this (in a little more complex environment and the solution with a lot of 'mouseenters' and 'mouseleaves' wasnt working properly) so i created a little jquery plugin that adds the method ismouseover. It has worked pretty well so far.
Then in any place of the document yo call it like this and it returns true or false:
I tested it on IE7+, Chrome 1+ and Firefox 4 and is working properly.
FIDDLE