So after reading a recently answered question i am unclear if i really understand the difference between the mouseenter()
and mouseover()
. The post states
MouseOver():
Will fire upon entering an element and whenever any mouse movements occur within the element.
MouseEnter():
Will fire upon entering an element.
I came up with a fiddle that uses both and they seem to be quite similar. Can someone please explain to me the difference between the two ?
I have also tried reading the JQuery definitions, both say the same thing.
The mouseover event is sent to an element when the mouse pointer enters the element
The mouseenter event is sent to an element when the mouse pointer enters the element.
Can someone please clarify with an example?
This question is related to
jquery
dom-events
The mouseenter event differs from mouseover in the way it handles event bubbling. The mouseenter event, only triggers its handler when the mouse enters the element it is bound to, not a descendant. Refer: https://api.jquery.com/mouseenter/
The mouseleave event differs from mouseout in the way it handles event bubbling. The mouseleave event, only triggers its handler when the mouse leaves the element it is bound to, not a descendant. Refer: https://api.jquery.com/mouseleave/
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 => {...});
This example demonstrates the difference between the mousemove, mouseenter and mouseover events:
https://jsfiddle.net/z8g613yd/
HTML:
<div onmousemove="myMoveFunction()">
<p>onmousemove: <br> <span id="demo">Mouse over me!</span></p>
</div>
<div onmouseenter="myEnterFunction()">
<p>onmouseenter: <br> <span id="demo2">Mouse over me!</span></p>
</div>
<div onmouseover="myOverFunction()">
<p>onmouseover: <br> <span id="demo3">Mouse over me!</span></p>
</div>
CSS:
div {
width: 200px;
height: 100px;
border: 1px solid black;
margin: 10px;
float: left;
padding: 30px;
text-align: center;
background-color: lightgray;
}
p {
background-color: white;
height: 50px;
}
p span {
background-color: #86fcd4;
padding: 0 20px;
}
JS:
var x = 0;
var y = 0;
var z = 0;
function myMoveFunction() {
document.getElementById("demo").innerHTML = z += 1;
}
function myEnterFunction() {
document.getElementById("demo2").innerHTML = x += 1;
}
function myOverFunction() {
document.getElementById("demo3").innerHTML = y += 1;
}
onmousemove
: occurs every time the mouse pointer is moved over the div element.onmouseenter
: only occurs when the mouse pointer enters the div element.onmouseover
: occurs when the mouse pointer enters the div element,
and its child elements (p and span).See the example code and demo at the bottom of the jquery documentation page:
http://api.jquery.com/mouseenter/
... mouseover fires when the pointer moves into the child element as well, while mouseenter fires only when the pointer moves into the bound element.
Though they operate the same way, however, the mouseenter
event only triggers when the mouse pointer enters the selected element. The mouseover
event is triggered if a mouse pointer enters any child elements as well.
Source: Stackoverflow.com