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);
}
Just a note about the popular and helpful Arthur Goldsmith answer above: If you're moving your mouse from one element to another in IE (at least until IE 9) you may have some trouble getting this to work correctly if the new element has a transparent background (which it would by default). My workaround was the give the new element a transparent background image.
You can test with
jQuery
if any child div has a certain class. Then by applying that class when you mouse over and out out a certain div, you can test whether your mouse is over it, even when you mouse over a different element on the page Much less code this way. I used this because I had spaces between divs in a pop-up, and I only wanted to close the pop up when I moved off of the pop up, not when I was moving my mouse over the spaces in the pop up. So I called a mouseover function on the content div (which the pop up was over), but it would only trigger the close function when I moused-over the content div, AND was outside the pop up!I combined ideas from this topic and came up with this, which is useful for showing/hiding a submenu:
Seems to work for me. Hope this helps someone.
EDIT: Now realizing this approach is not working correctly in IE.
As I cannot comment, so I will write this as an answer!
Please understand the difference between css selector ":hover" and the hover event!
":hover" is a css selector and was indeed removed with the event when used like this
$("#elementId").is(":hover")
, but in it's meaning it has really nothing to do with the jQuery event hover.if you code
$("#elementId:hover")
, the element will only be selected when you hover with the mouse. the above statement will work with all jQuery versions as your selecting this element with pure and legit css selection.On the other hand the event hover which is
is indeed deprecaded as jQuery 1.8 here the state from jQuery website:
Why they removed the usage is(":hover") is unclear but oh well, you can still use it like above and here is a little hack to still use it.
Oh and I would not recomment the timeout version as this brings a lot of complexity, use timeout functionalities for this kind of stuff if there is no other way and believe me, in 95% percent of all cases there is another way!
Hope I could help a couple people out there.
Greetz Andy
I couldn't use any of the suggestions above.
Why I prefer my solution?
This method checks if mouse is over an element at any time chosen by You.
Mouseenter and :hover are cool, but mouseenter triggers only if you move the mouse, not when element moves under the mouse.
:hover is pretty sweet but ... IE
So I do this:
No 1. store mouse x, y position every time it's moved when you need to,
No 2. check if mouse is over any of elements that match the query do stuff ... like trigger a mouseenter event
I made a jQuery plugin that can do this and a lot more. In my plugin, to get all elements the cursor is currently hovered over, simply do the following:
As I mentioned, it also has alot of other uses as you can see in the
jsFiddle found here