How to check if the mouse is over an element? (Jquery)

How to check if the mouse is over an element?

I move the item to the cursor position and I need to check if the mouse is over another item. How to do it?

<div class="dragged"></div> // I can move it <div class="dropped"></div> // Need to drop here 
 $('.dragged').mousemove(function(e) { ... if($(".dropped").is(":hover")) { // of course doesn't work ... } }); 

Thanks in advance.

+6
source share
5 answers

You can try the following:

 $('#test').click(function() { if ($('#Test').is(':hover')) { alert('Test'); } }); 

JS FIDDLE DEMO

+8
source

One valid approach is the β€œflag” of the .dropped tag element on mouse input. Finally, when you move .dragged, you can verify that .dropped has a tag that you insert into them.

 $('.dropped').hover(function() { $(this).addClass('hovered'); }, function() { $(this).removeClass('hovered'); }); $('.dragged').mousemove(function(e) { ... if($(".dropped").hasClass(".hovered")) { ... } }); 

Sincerely.

+2
source

try the following:

  var hovred=null; $('.dropped').mouseenter(function(e) { hovred=$(this); }); $('.dropped').mouseleave(function(e) { hovred=null; }); $('.dropped').mousemove(function(e) { ... if($(this)==hovred && hovred != null) { //do your stuff here man } }); 
+2
source

Do something like that.

  var $container = $('.container'); $container.mouseenter(function(e) { //do something }); 
0
source
 <script type="text/javascript"> function call_mouseover() { alert("mouse is over on the div"); } function call_mouseout() { alert("mouse out from the div"); } </script> <div class="dragged" onmouseover="call_mouseover();" onmouseout="call_mouseout();"></div> <div class="dropped" ></div> // Need to drop here 
0
source

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


All Articles