Moving the Mouse: mouseover, mouseout, mouseenter, mouseleave in JavaScript

Moving the Mouse: mouseover, mouseout, mouseenter, mouseleave in JavaScript

Moving the Mouse: mouseover, mouseout, mouseenter, mouseleave in JavaScript

Mouse events in JavaScript help detect when the cursor moves over elements. The four key events related to this are:

  1. mouseover – Triggers when the mouse enters an element or its child elements.
  2. mouseout – Triggers when the mouse leaves an element or its child elements.
  3. mouseenter – Triggers when the mouse enters an element but not its children.
  4. mouseleave – Triggers when the mouse leaves an element but not its children.

šŸ”¹ Difference Between mouseover / mouseout and mouseenter / mouseleave

EventTriggers when movingAffects child elements?Bubbling?
mouseoverInto an element or its children✅ Yes✅ Yes
mouseoutOut of an element or its children✅ Yes✅ Yes
mouseenterInto an element only❌ No❌ No
mouseleaveOut of an element only❌ No❌ No

šŸ”¹ Example 1: mouseover and mouseout

These events trigger when moving over or out of an element or its child elements.

let box = document.getElementById("box"); box.addEventListener("mouseover", function() { box.style.backgroundColor = "lightblue"; console.log("Mouse entered (mouseover)"); }); box.addEventListener("mouseout", function() { box.style.backgroundColor = "lightcoral"; console.log("Mouse left (mouseout)"); });

HTML:

<div id="box" style="width:200px; height:100px; background:lightcoral; text-align:center; line-height:100px;"> Hover me! </div>

šŸ›  Effect:

  • mouseover: Changes background to light blue when the mouse enters the div.
  • mouseout: Changes background to light coral when the mouse leaves.

Works on child elements too!

šŸ”¹ Example 2: mouseenter and mouseleave (No Bubbling!)

These events fire only when entering/leaving the element itself, not its children.

let box = document.getElementById("box"); box.addEventListener("mouseenter", function() { box.style.backgroundColor = "lightgreen"; console.log("Mouse entered (mouseenter)"); }); box.addEventListener("mouseleave", function() { box.style.backgroundColor = "lightcoral"; console.log("Mouse left (mouseleave)"); });

šŸ›  Effect:

  • mouseenter: Background turns light green when the mouse enters the div.
  • mouseleave: Background turns light coral when the mouse leaves.

Ignores child elements (unlike mouseover/mouseout).

šŸ”¹ Example 3: Detect Mouse Movement Inside an Element

We can track mouse movement inside an element using mousemove.

let box = document.getElementById("box"); box.addEventListener("mousemove", function(event) { console.log(`Mouse at: X=${event.clientX}, Y=${event.clientY}`); });

šŸ›  Effect: Logs the mouse coordinates when moving inside the box.

šŸ”¹ When to Use These Events?

mouseover / mouseout → Use when detecting hover including child elements (e.g., dropdown menus).
mouseenter / mouseleave → Use when detecting hover only on the element itself (e.g., tooltips).
mousemove → Use for tracking mouse position (e.g., custom cursors, effects).

šŸš€ Understanding these events helps create smooth user interactions and animations!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close