Mouse Events
UI Events: Mouse Events
What is a mouse event in JavaScript?
View Answer:
What is the event order of a mouse left-click in the browser?
View Answer:
What's the difference between 'click' and 'dblclick' events?
View Answer:
If a user is on a MAC, what is the event property key for their OS?
View Answer:
document.addEventListener('keydown', function(event) {
if (event.metaKey) {
console.log('Command key was pressed');
}
});
In this code, when a key is pressed, if it's the Command key, the message 'Command key was pressed' will be logged to the console.
What is the 'mousemove' event?
View Answer:
document.addEventListener('mousemove', function(event) {
console.log(`Mouse position: X = ${event.clientX}, Y = ${event.clientY}`);
});
This code logs the current mouse position in the console every time the mouse moves.
Can you name some mouse button events?
View Answer:
Can you explain the difference between 'mouseover' and 'mouseenter' events?
View Answer:
<div id="parent">
Parent
<div id="child">Child</div>
</div>
<script>
document.getElementById('parent').addEventListener('mouseover', function() {
console.log('Mouseover on parent');
});
document.getElementById('parent').addEventListener('mouseenter', function() {
console.log('Mouseenter on parent');
});
document.getElementById('child').addEventListener('mouseover', function() {
console.log('Mouseover on child');
});
document.getElementById('child').addEventListener('mouseenter', function() {
console.log('Mouseenter on child');
});
</script>
In this example, moving the mouse over the 'Child' div will trigger both the 'Mouseover on child' and 'Mouseover on parent' logs, but only the 'Mouseenter on child' log, not the 'Mouseenter on parent'.
What differentiates window and document relative mouse positions?
View Answer:
document.addEventListener('mousemove', function(event) {
console.log(`Window-relative position: X = ${event.clientX}, Y = ${event.clientY}`);
console.log(`Document-relative position: X = ${event.pageX}, Y = ${event.pageY}`);
});
In this code, every time the mouse moves, it logs the current mouse position relative to the window (viewport) and the document (page including scroll).
A double mouse click has the unintended consequence of selecting that text. Is there a method to disable this particular behavior?
View Answer:
<!-- Before... -->
<b ondblclick="console.log('Click!')" onmousedown="return false"> Double-click me </b>
<!-- ...After -->
Can you block text or image copying in a browser document?
View Answer:
<div oncopy="console.log('Copying forbidden!'); return false">
Dear user, The copying is forbidden for you. If you know JS or HTML, then you
can extract everything from the page source.
</div>
What does 'event bubbling' mean in the context of mouse events?
View Answer:
What is the use of the 'event.preventDefault()' method in mouse events?
View Answer:
Can you explain what 'contextmenu' event is?
View Answer:
document.addEventListener('contextmenu', function(event) {
event.preventDefault(); // Prevents the default context menu from appearing
console.log('Context menu was triggered');
});
In this code, when the user right-clicks on the document, the default context menu is prevented from appearing, and the message 'Context menu was triggered' is logged to the console. You can perform custom actions or show a custom context menu instead of the default behavior.
What is the difference between 'mouseleave' and 'mouseout' events?
View Answer:
How does 'event.stopPropagation()' affect mouse events?
View Answer:
<div id="parent">
Parent Element
<button id="child">Click Me!</button>
</div>
<script>
document.getElementById('parent').addEventListener('click', function() {
console.log('Parent element clicked');
});
document.getElementById('child').addEventListener('click', function(event) {
event.stopPropagation(); // Stop event propagation to parent elements
console.log('Child element clicked');
});
</script>
What do the 'clientX' and 'clientY' properties represent in a mouse event?
View Answer:
document.addEventListener('mousemove', function(event) {
console.log(`Mouse position: X = ${event.clientX}, Y = ${event.clientY}`);
});
Output:
"Mouse position: X = 90, Y = 84"
"Mouse position: X = 93, Y = 79"
"Mouse position: X = 95, Y = 77"
"Mouse position: X = 100, Y = 71"
"Mouse position: X = 101, Y = 68"
"Mouse position: X = 101, Y = 65"
"Mouse position: X = 101, Y = 59"
In this code, whenever the mouse moves, it logs the current X and Y coordinates of the mouse pointer relative to the viewport (browser window) in the console.
What are 'screenX' and 'screenY' properties?
View Answer:
document.addEventListener('mousemove', function(event) {
console.log(`Mouse position: X = ${event.screenX}, Y = ${event.screenY}`);
});
Output:
"Mouse position: X = 138, Y = 152"
"Mouse position: X = 141, Y = 156"
"Mouse position: X = 148, Y = 163"
"Mouse position: X = 164, Y = 179"
"Mouse position: X = 169, Y = 187"
"Mouse position: X = 169, Y = 195"
In this code, whenever the mouse moves, it logs the current X and Y coordinates of the mouse pointer relative to the screen or monitor in the console, regardless of the browser window or viewport.
Can you explain the 'pointerlockchange' event?
View Answer:
document.addEventListener('pointerlockchange', function() {
if (document.pointerLockElement) {
console.log('The pointer is locked');
} else {
console.log('The pointer is not locked');
}
});
// To request pointer lock
document.body.requestPointerLock();
In this code, the pointerlockchange
event handler checks if document.pointerLockElement
is set, which would indicate that the pointer is locked. If not, it's unlocked.
What does 'buttons' property represent in a mouse event?
View Answer:
document.addEventListener('mousedown', function(e) {
if (e.buttons == 1) {
console.log('Left button is pressed');
} else if (e.buttons == 2) {
console.log('Right button is pressed');
} else if (e.buttons == 4) {
console.log('Middle button is pressed');
}
});
In this example, when a mousedown
event is fired, the event handler checks the buttons
property to see which mouse button was pressed. It logs a message depending on whether the left, right, or middle button was pressed.
What does 'detail' property represent in a 'mousedown' or 'mouseup' event?
View Answer:
document.addEventListener('mousedown', function(e) {
console.log(`The mouse button was pressed ${e.detail} time(s)`);
});
document.addEventListener('mouseup', function(e) {
console.log(`The mouse button was released ${e.detail} time(s)`);
});
In this example, when a mousedown
or mouseup
event is fired, the event handler checks the detail
property and logs a message indicating how many times the mouse button has been pressed and released.
What is the 'auxclick' event?
View Answer:
document.addEventListener('auxclick', function(e) {
if (e.button === 1) {
console.log('Middle button was clicked.');
}
});
In this example, when an auxclick
event is fired, the event handler checks if the middle mouse button was clicked and logs a message if it was. Please note that not all mice have a middle button.
What is the 'pointerId' property in a mouse event?
View Answer:
document.addEventListener('pointerdown', function(e) {
console.log(`The pointer id is ${e.pointerId}`);
});
In this example, when a pointerdown
event is fired, the event handler logs the unique identifier of the pointer that triggered the event. This could be useful for distinguishing between different pointers in a multi-touch scenario, for example.
How can you detect the direction of the mouse wheel scroll?
View Answer:
document.addEventListener('wheel', function(e) {
if (e.deltaY < 0) {
console.log('Scrolled up');
} else {
console.log('Scrolled down');
}
});
In this example, when a wheel
event is fired, the event handler checks the deltaY
property to determine the scroll direction. It logs a message depending on whether the user scrolled up or down.