离开页面时悬停状态保持不变 - 忽略 mouseleave
我使用一些相当标准的 JavaScript/jQuery 来处理悬停元素、图像交换、滑动 div、动画等,这并不重要。如果/当单击“可悬停”链接元素会将您带到新页面时,mouseenter 悬停状态将始终保持不变。
例如,如果您将鼠标悬停在某个内容上并单击它(链接到另一个页面),然后使用后退按钮返回到该页面,则您单击的元素上的 mouseenter 状态将被卡住,即使您的鼠标不再位于该元素上方。
您必须重新加载页面或重新悬停元素才能重置所有内容。
$(document).ready(function() {
$('.mySelector').each(function () {
$(this).hover(enter, leave);
});
function enter(event) {
// mouseenter stuff
};
function leave(event) {
// mouseleave stuff
};
});
我似乎记得几周前读到过这个问题,有一个非常简单的修复方法,但我再也找不到了。
有人熟悉正确的解决方案吗?
谢谢!
I'm using some pretty standard JavaScript/jQuery to handle hovering elements, image swaps, sliding divs, animations, etc., it does not matter. If/when clicking an "hoverable" linked element takes you to a new page, the mouseenter hover state always sticks.
For example, if you hover over something and click it (links to another page), then use the back button to return to the page, the mouseenter state on the element you clicked, is stuck even though your mouse is no longer over the element.
You have to either reload the page or re-hover the element to reset everything.
$(document).ready(function() {
$('.mySelector').each(function () {
$(this).hover(enter, leave);
});
function enter(event) {
// mouseenter stuff
};
function leave(event) {
// mouseleave stuff
};
});
I seem to remember reading about this several weeks ago and there was a very simple fix but I can no longer find that.
Is anyone familiar with a proper solution?
Thank-you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要为此使用
.each
。此外,这些函数应该位于.ready
之外。编辑:
如果你的变量是本地的,你可以这样做:
You don't need to use
.each
for this. Also, the functions should be outside.ready
.Edit:
If your variables are local, you could do it like this:
我最终只是通过调用带有窗口“卸载”的鼠标离开函数来“重新设置”悬停效果...
每当您通过单击悬停元素离开页面时,mouseleave即使您的鼠标仍然悬停在该元素上, 函数也会被调用。点击浏览器的后退按钮不再会将您带回具有“卡住”悬停效果的页面。
问题已解决。
I ended up simply "re-setting" the hover effects by calling the mouse leave function with the window "unload"...
Whenever you leave the page by clicking the hovered element, the mouseleave function is called even though your mouse is still hovering over the element. Hitting the browser's back button no longer takes you back to the page with a "stuck" hover effect.
Problem solved.