How do I check if the mouse is over an element in jQuery?

前端 未结 24 2106
不思量自难忘°
不思量自难忘° 2020-11-22 08:10

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

24条回答
  •  有刺的猬
    2020-11-22 09:04

    Thanks to both of you. At some point I had to give up on trying to detect if the mouse was still over the element. I know it's possible, but may require too much code to accomplish.

    It took me a little while but I took both of your suggestions and came up with something that would work for me.

    Here's a simplified (but functional) example:

    $("[HoverHelp]").hover (
        function () {
            var HelpID = "#" + $(this).attr("HoverHelp");
            $(HelpID).css("top", $(this).position().top + 25);
            $(HelpID).css("left", $(this).position().left);
            $(HelpID).attr("fadeout", "false");
            $(HelpID).fadeIn();
        },
        function () {
            var HelpID = "#" + $(this).attr("HoverHelp");
            $(HelpID).attr("fadeout", "true");
            setTimeout(function() { if ($(HelpID).attr("fadeout") == "true") $(HelpID).fadeOut(); }, 100);
        }
    );
    

    And then to make this work on some text this is all I have to do:

    
    
    This is a mouse over effect.
    

    Along with a lot of fancy CSS, this allows some very nice mouseover help tooltips. By the way, I needed the delay in the mouseout because of tiny gaps between checkboxes and text that was causing the help to flash as you move the mouse across. But this works like a charm. I also did something similar for the focus/blur events.

提交回复
热议问题