How to determine if a mouse is hovering over an element

Is there a function that I can call to find out if a particular element is currently located, for example?

/* Returns true or false */ hoveringOver("a#mylink"); 
+4
source share
3 answers

You can use jQuery hover method to track:

 $(...).hover( function() { $.data(this, 'hover', true); }, function() { $.data(this, 'hover', false); } ).data('hover', false); if ($(something).data('hover')) //Hovered! 
+7
source

Yes, in classic JS:

 document.getElementById("mylink").onmouseover = function() { alert("Hover!"); } document.getElementById("mylink").onmouseout = function() { alert("Out!"); } 

in jQuery:

 $('#mylink').mouseover(function() { alert("Hover!"); }); 
+3
source

I have no idea if this was the best way, but if you use jquery you can do something like this:

 var hoveringOver = false; $("a#myLink").bind("mouseenter", function(){ hoveringOver = true; }); $("a#myLink").bind("mouseleave", function(){ hoveringOver = false; }); function isHoveringOver(){ return hoveringOver; } 
+1
source

Source: https://habr.com/ru/post/1305855/


All Articles