Mouse event
Mouse events in JavaScript are events triggered by user interactions with a web page using a mouse. There are several types of mouse events, and you can use them to capture user actions like clicks, movements, and more. Here are some common mouse events in JavaScript.
click: Triggered when the user clicks the left mouse button.
element.addEventListener('click', function(event) {
// Your code here
});
dblclick: Triggered when the user double-clicks the left mouse button.
element.addEventListener('dblclick', function(event) {
// Your code here
});
mousedown: Triggered when the user presses any mouse button.
element.addEventListener('mousedown', function(event) {
// Your code here
});
mouseup: Triggered when the user releases a mouse button.
element.addEventListener('mouseup', function(event) {
// Your code here
});
mousemove: Triggered when the user moves the mouse pointer over an element.
element.addEventListener('mousemove', function(event) {
// Your code here
});
mouseover and mouseout: Triggered when the mouse enters or leaves an element.
element.addEventListener('mouseover', function(event) {
// Mouse entered the element
});
element.addEventListener('mouseout', function(event) {
// Mouse left the element
});
mouseenter and mouseleave: Like mouseover and mouseout but do not bubble up the DOM hierarchy.
element.addEventListener('mouseenter', function(event) {
// Mouse entered the element
});
element.addEventListener('mouseleave', function(event) {
// Mouse left the element
});
contextmenu: Triggered when the user right-clicks on an element.
element.addEventListener('contextmenu', function(event) {
// Your code here
event.preventDefault(); // Prevents the default context menu
});
Also note that, in each event listener, the event parameter provides information about the event, such as the mouse position, target element, and more. Additionally, you can use methods like preventDefault() to control the default behavior of some events.
The below-mentioned example using HTML CSS & JS will demonstrate the various mouse events in JavaScript.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mouse Events Example</title>
<style>
#mouseEventsDiv {
width: 200px;
height: 100px;
background-color: lightblue;
margin: 50px;
text-align: center;
line-height: 100px;
}
</style>
</head>
<body>
<div id="mouseEventsDiv">Hover over me!</div>
<script>
// Get the target element
var mouseEventsDiv = document.getElementById('mouseEventsDiv');
// Click event
mouseEventsDiv.addEventListener('click', function(event) {
alert('Click event!');
});
// Double-click event
mouseEventsDiv.addEventListener('dblclick', function(event) {
alert('Double-click event!');
});
// Mouse down event
mouseEventsDiv.addEventListener('mousedown', function(event) {
console.log('Mouse down event!');
});
// Mouse up event
mouseEventsDiv.addEventListener('mouseup', function(event) {
console.log('Mouse up event!');
});
// Mouse move event
mouseEventsDiv.addEventListener('mousemove', function(event) {
console.log('Mouse move event at (' + event.clientX + ', ' + event.clientY + ')');
});
// Mouse over event
mouseEventsDiv.addEventListener('mouseover', function(event) {
console.log('Mouse over event!');
});
// Mouse out event
mouseEventsDiv.addEventListener('mouseout', function(event) {
console.log('Mouse out event!');
});
// Mouse enter event
mouseEventsDiv.addEventListener('mouseenter', function(event) {
console.log('Mouse enter event!');
});
// Mouse leave event
mouseEventsDiv.addEventListener('mouseleave', function(event) {
console.log('Mouse leave event!');
});
// Context menu event
mouseEventsDiv.addEventListener('contextmenu', function(event) {
alert('Context menu event!');
event.preventDefault(); // Prevent the default context menu
});
</script>
</body>
</html>