移动 div 后更新鼠标与 div 的交互

发布于 2025-01-14 07:46:58 字数 913 浏览 4 评论 0原文

在我正在构建的网站中,我有一个带有 :hover 动画的按钮,当您单击该按钮时,页面上的很多内容(包括按钮)都会移动。我的问题是,即使按钮从鼠标下方移出,它也不会更新并失去其:悬停效果,直到您再次移动鼠标。

这是一个示例 - 一旦您单击按钮,它就会保持浅蓝色(悬停颜色),直到您再次移动鼠标。

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>

如何在用户不需要再次移动鼠标的情况下更新元素? jQuery 没问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗弃M 2025-01-21 07:46:58

我认为用你目前的 CSS 方法你将无法处理这个问题。尝试另一种 JavaScript 方法:

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>

如果将“悬停”提取到 JS 代码中,则无需用户移动鼠标即可更新元素的状态。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文