4

Simple question, how would I accomplish this functionality in Jquery: Test whether the mouse is hovering over .myBox

    if ($(".myBox").mouseleave = true) {
        DO SOMETHING
    } else {something else}

OR

    if ($(".myBox").mouseover = false) {
        DO SOMETHING
    } else {Something else}

NOTE: im looking for an IF statement

android.nick
  • 11,069
  • 23
  • 77
  • 112

8 Answers8

10

jQuery provides the is method for checking conditions with regard to a jQuery object. In your case you can check for the :hover CSS pseudo class:

$('.myBox').is(':hover') === true

Havre a look at this demo, try clicking the button (which will alert true) and the tabbing to and hitting enter on the button (without the mouse, it will return false).

DEMO: http://jsfiddle.net/marcuswhybrow/LL5JD/

Marcus Whybrow
  • 19,578
  • 9
  • 70
  • 90
4

Take a look at jQuery Mouse Over

$(".my_box").mouseover(function(){
    // Mouse over...
});

$(".my_box").mouseout(function(){
    // Mouse left...
});

Here is an example, adding a border to an image when hovering over it, and removing it after x amount of time if its not been re-hovered: See it working here

var hover_off = false;
var hover_count = 1000;

$(".my_box").mouseover(function() {
    hover_off = false;
    $(this).addClass('hovering');
});

$(".my_box").mouseout(function() {
    hover_off = true;
    setTimeout(myMouseOut, hover_count);
});

function myMouseOut() {
    if (hover_off) {
        $(".my_box").removeClass('hovering');
    }
}
Scoobler
  • 9,696
  • 4
  • 36
  • 51
2

That's right, $('.myBox').is(':hover') used with jQuery 1.5.1 throws the error, but in my tests only in Opera Browser (11.01): Uncaught exception: Syntax error, unrecognized expression: hover

A workaround is using a negated expression: $('.myBox').is('not(:hover)') That worked fine while my tests in Opera 11, FF4, IE7, Chrome 5.

1

This did the trick for me:

var hover = false;
$('.someClass').each(function(i,e){
    hover = !hover ? $(e).is(':hover') : true;
});

if (hover)
    // do something
else
    // do something else
1

Use .hover() http://api.jquery.com/hover/

CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
1
$(".myBox").hover(
           function(){
               //DO SOMETHING ON MOUSE ENTER
           },
           function(){
               //DO SOMETHING ON MOUSE LEAVE
           }
 });
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
  • 1
    Actually this is unbelievably useful. This structure acts as an else clause where ifs cannot be used, for example, when you cannot (coincidentally) use `.hover` with an if because you are struggling with chrome's autocomplete in a pop up login form. If you use `.hover` you will surely get undesired behaviour, instead when you use `$("#foo").mouseout(function(){#The Code},function(){});` you have no problems at all. Thank you. – Luis May 05 '17 at 02:49
  • Well said @Luis as for chrome autofill fiascos, someone said that wrapping text inputs in `
    ` tags stop it. I tried it and it works!
    – TheVillageIdiot May 05 '17 at 02:51
0

use this: http://plugins.jquery.com/project/enterleave-events

$('.myBox').bind('enter leave',function() {
  // do something, use $(this) to get the object
});
Syl
  • 2,733
  • 2
  • 17
  • 20
0

Act on the mouseenter and mouseleave events.

Or use the hover which can handle both of them..

$('.myBox').hover(
   function(e){
     //do the mouseenter things here...
   },
   function(e){
     //do the mouseleave things here...
   }
);

Example at http://www.jsfiddle.net/gaby/ECYD4/

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • im trying to create a delay when i hover off of something, so it doesn't close right away, but after maybe 3 seconds, but if i hover off, then hover back on, cancel hover off, so if someone accidentally hovers off, it doesn't close right away. – android.nick Jan 28 '11 at 17:07
  • @android.nick See the update to my question for an example how to adapt Gaby's code to do what you want. jsfiddle.net/Scoobler/ThLqx/12 – Scoobler Jan 28 '11 at 18:25