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

139
Views
Update mouse interaction with div after moving it

In a website I'm building, I have a button with a :hover animation, and when you click the button a lot of things on the page including the button move around. My problem is that even after the button moves out from under the mouse, it doesn't update and lose its :hover effect until you move the mouse again.

Here's an example - here once you click the button it stays light blue (the hovered colour) until you move the mouse again.

function clicked() {
  if (document.getElementById('mydiv').style.transform == 'translateY(70px)') {
    document.getElementById('mydiv').style.transform = 'translateY(0px)';
  } else {
    document.getElementById('mydiv').style.transform = 'translateY(70px)';
  }
};
div {
  background-color: #fedcba;
  padding: 20px;
  display: inline-block;
}
div:hover {
  background-color: #abcdef;
}
<div id="mydiv" onclick="clicked();">Click me</div>

How do I make the element update without the user needing to move the mouse again? JQuery is ok.

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

0

I think with your current CSS approach you won't be able to handle that. Try another JavaScript approach:

const el = document.getElementById('mydiv');

el.addEventListener('click', () => {
  if(el.style.transform == 'translateY(50px)') {
    el.style.transform = 'translateY(0px)';
  } else {
    el.style.transform = 'translateY(50px)';
  }
  el.style.background = "#fedcba";
});

el.addEventListener('mouseover', () => {
  el.style.background = "#abcdef";
});

el.addEventListener('mouseout', () => {
  el.style.background = "#fedcba";
});
div {
  background: #fedcba;
  padding: 20px;
  display: inline-block;
}
<div id="mydiv">Click me</div>

If you extract the "hover" into the JS code then you can update the state of the element without the user having to move the mouse.

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!