Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

361
Views
document mouseover & mouseout firing multiple times - should just fire once

I need to fire two events. One when the cursor enters the document and when he leaves the document. Right now i have a problem that the events also fire when i use the cursor inside the document, you can see this in the console of my fiddle.

How is it possible to just fire the events when i enter and leave the document?

let cursor = document.getElementById('cursor');

function onMousEnter() {
    cursor.classList.add('is-active');
    console.log("Enter!");
}

function onMouseLeave() {
    cursor.classList.remove('is-active');
    console.log("Out!");
}

document.addEventListener('mouseover', onMousEnter);;
document.addEventListener('mouseout', onMouseLeave);

const onMouseMove = (e) => {
    cursor.style.left = e.pageX + 'px';
    cursor.style.top = e.pageY + 'px';  
}

document.addEventListener('mousemove', onMouseMove);

https://jsfiddle.net/k81zbdmj/1/

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. Use mouseenter instead of mouseover.
  2. Use mouseleave instead of mouseout.

The difference between those is that mouseenter and mouseleave don't bubble. So whenever you move your cursor over or out of a child, those won't trigger the event.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!