How to check if mouse is over an element?
I moving element to cursor position and I need to check if mouse is over an another element. 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.
You can simply use the CSS :hover pseudo-class selector in combination with the jQuery mousemove() to check whether the mouse is over an element or not in jQuery.
The mouseover event triggers when the mouse pointer enters the div element, and its child elements. The mouseenter event is only triggered when the mouse pointer enters the div element.
jQuery mouseout() MethodThe mouseout event occurs when the mouse pointer leaves the selected element. The mouseout() method triggers the mouseout event, or attaches a function to run when a mouseout event occurs.
mouseover: The onmouseover event triggers when the mouse pointer enters an element or any one of its child elements. mouseenter: The onmouseenter event is triggered only when the mouse pointer hits the element.
You may try like this:
$('#test').click(function() {
if ($('#Test').is(':hover')) {
alert('Test');
}
});
JS FIDDLE DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With