jQuery mouse events allow you to detect and respond to user interactions performed using the mouse. These events are commonly used to create interactive UI behaviors like hover effects, click actions, and drag-based interactions.
Mouse events in jQuery are attached using event methods or the .on() method. These events execute callback functions when the specified mouse interaction occurs on the selected element.
// Basic click event example
$("#btn").click(function () {
alert("Button clicked");
});
// Mouse enter and leave example
$("#box").mouseenter(function () {
$(this).css("background", "green");
}).mouseleave(function () {
$(this).css("background", "red");
});
The following example demonstrates real-time mouse movement tracking.
// Display mouse coordinates inside a div
$(document).mousemove(function (e) {
$("#coords").text("X: " + e.pageX + " Y: " + e.pageY);
});
mouseenter over mouseover when bubbling is not requiredmousemove for performance reasons