Old question, but still no good up-to-date answer with insight imo.
As jQuery uses Javascript wording for events and handlers, but does its own undocumented, but different interpretation of those, let me first shed light on the difference from the pure Javascript viewpoint:
enter/over
gets a corresponding leave/out
(possibly late/jumpy)mouseenter/mouseleave
mouseenter/mouseleave
cycle (i.e. no events fire)mouseenter/mouseleave
event cyclesmouseenter/mouseleave
could look like, you end up with with something like mouseover/mouseout
mouseover/mouseout
mouseout
on the previously sampled elementmouseover
on the new elementtarget/relatedTarget
indicate new and previous element$(event.target).closest(...)
suits your needsNot-so-trivial mouseover/mouseout
example:
$('.side-menu, .top-widget')
.on('mouseover mouseout', event => {
const target = event.type === 'mouseover' ? event.target : event.relatedTarget;
const thing = $(target).closest('[data-thing]').attr('data-thing') || 'default';
// do something with `thing`
});
These days, all browsers support mouseover/mouseout
and mouseenter/mouseleave
natively. Nevertheless, jQuery does not register your handler to mouseenter/mouseleave
, but silently puts them on a wrappers around mouseover/mouseout
as the code below exposes.
The emulation is unnecessary, imperfect and a waste of CPU cycles: it filters out mouseover/mouseout
events that a mouseenter/mouseleave
would not get, but the target
is messed. The real mouseenter/mouseleave
would give the handler element as target, the emulation might indicate children of that element, i.e. whatever the mouseover/mouseout
carried.
For that reason I do not use jQuery for those events, but e.g.:
$el[0].addEventListener('mouseover', e => ...);
const list = document.getElementById('log');
const outer = document.getElementById('outer');
const $outer = $(outer);
function log(tag, event) {
const li = list.insertBefore(document.createElement('li'), list.firstChild);
// only jQuery handlers have originalEvent
const e = event.originalEvent || event;
li.append(`${tag} got ${e.type} on ${e.target.id}`);
}
outer.addEventListener('mouseenter', log.bind(null, 'JSmouseenter'));
$outer.on('mouseenter', log.bind(null, '$mouseenter'));
_x000D_
div {
margin: 20px;
border: solid black 2px;
}
#inner {
min-height: 80px;
}
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<div id=outer>
<ul id=log>
</ul>
</div>
</body>
_x000D_
Note: For delegate handlers never use jQuery’s “delegate handlers with selector registration”. (Reason in another answer.) Use this (or similar):
$(parent).on("mouseover", e => {
if ($(e.target).closest('.gold').length) {...};
});
instead of
$(parent).on("mouseover", '.gold', e => {...});