Вот небольшой jQuery плагин , который проверяет, находится ли мышь над элементом.
Использование:
$("#YourElement").isMouseOverMe();
Пример:
(function($) {
var mx = 0;
var my = 0;
$(document).mousemove(function(e) { // no expensive logic here
mx = e.clientX;
my = e.clientY;
})
$.fn.isMouseOverMe = function() {
var $el = $(this);
var el_xmin = $el.offset().left;
var el_ymin = $el.offset().top;
var el_xmax = el_xmin + $el.width();
var el_ymax = el_ymin + $el.height();
return mx >= el_xmin && mx <= el_xmax && my >= el_ymin && my <= el_ymax;
};
}(jQuery));
$(document).mouseup(function(e) {
console.log($("#div").isMouseOverMe())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Click inside or outside of the yellow box</h2>
<div id="div" style="width:200px;height:200px;background-color:yellow;margin-top:50px"></div>