移动 div 后更新鼠标与 div 的交互
在我正在构建的网站中,我有一个带有 :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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为用你目前的 CSS 方法你将无法处理这个问题。尝试另一种 JavaScript 方法:
如果将“悬停”提取到 JS 代码中,则无需用户移动鼠标即可更新元素的状态。
I think with your current CSS approach you won't be able to handle that. Try another JavaScript approach:
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.