离开页面时悬停状态保持不变 - 忽略 mouseleave

发布于 2024-11-06 09:22:18 字数 656 浏览 5 评论 0原文

我使用一些相当标准的 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 技术交流群。

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

发布评论

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

评论(2

平定天下 2024-11-13 09:22:18

您不需要为此使用 .each 。此外,这些函数应该位于 .ready 之外。

$(document).ready(function() {
    $('.mySelector').hover(enter, leave);
});

function enter(event) { 
    // mouseenter stuff
}

function leave(event) { 
    // mouseleave stuff
}

编辑:
如果你的变量是本地的,你可以这样做:

$(document).ready(function() {
     $('.mySelector').hover(function(){
         // mouseenter stuff
     },
     function(){
         // mouseleave stuff
     });
});

You don't need to use .each for this. Also, the functions should be outside .ready.

$(document).ready(function() {
    $('.mySelector').hover(enter, leave);
});

function enter(event) { 
    // mouseenter stuff
}

function leave(event) { 
    // mouseleave stuff
}

Edit:
If your variables are local, you could do it like this:

$(document).ready(function() {
     $('.mySelector').hover(function(){
         // mouseenter stuff
     },
     function(){
         // mouseleave stuff
     });
});
蓝颜夕 2024-11-13 09:22:18

我最终只是通过调用带有窗口“卸载”的鼠标离开函数来“重新设置”悬停效果...

$(window).unload(function() {
    leave();
});

每当您通过单击悬停元素离开页面时,mouseleave即使您的鼠标仍然悬停在该元素上, 函数也会被调用。点击浏览器的后退按钮不再会将您带回具有“卡住”悬停效果的页面。

问题已解决。

I ended up simply "re-setting" the hover effects by calling the mouse leave function with the window "unload"...

$(window).unload(function() {
    leave();
});

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.

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