jQuery /hover()/.mousemove() 优化问题

发布于 2024-12-28 01:51:07 字数 414 浏览 1 评论 0原文

我正在研究一个基本的模式示例,其中只要用户将鼠标悬停在某个部分上,模式就会跟随鼠标光标。

我遇到的问题是,当从左向右移动时,模式会显着滞后,并且如果用户将鼠标移出特定部分,还会触发 fadeOut() 设置。

从右到左,无缝工作。

在小提琴中,您可以观察到将鼠标从上到下移动到

如果有任何重复的问题或你们都知道的相关文章,请给我指出正确的方向。到目前为止,我的搜索仅供参考,但尚未解决这个具体问题。

这是我的小提琴

谢谢大家,一如既往地热烈欢迎专业人士的建议。

I'm working on a basic modal example in which the modal would follow the mouse cursor as long as the user was hovering over a certain section.

The issue I'm having is, when going from left to right, the modal lags significantly, as well as triggers the fadeOut() set if the user were to mouse out of the specific section.

Right to left, works seamlessly.

In the fiddle, you can observe the lagginess when moving the mouse over the <nav> from top to bottom, as well as notice the solid performance from bottom to top.

If there are any duplicate questions or related articles that you all know of, please point me in the right direction. My searches thus far have been informational but have not adressed this specific issue.

Here is my fiddle.

Thank you all, as always pro advice is warmly welcomed.

Ken

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

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

发布评论

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

评论(3

心病无药医 2025-01-04 01:51:07

问题是,当您添加模态元素时,jquery 会触发 onmouseout 事件,它会获得“焦点”,并且悬停事件不会在您的导航元素上触发。
我更改了您的示例,使其现在工作得更好,请查看此处

The issue is that jquery fires onmouseout event when you add the modal element it gets "focus" and hover events are not firing on your nav element.
I altered your example so that it is wokring much better now, check it out here

站稳脚跟 2025-01-04 01:51:07

哦,太棒了,经典的例子!问题在于,一旦您触摸 section#coming-soon,jQuery 就会认为您已将鼠标移出,因此它显然会运行 fadeOut...

一种克服方法这是为了将 section#coming-soon 放入 nav 中,这样脚本仍会认为您位于 section#coming-soon 内code>,即使您将鼠标悬停在其上:

<nav>
    <ul>
        <li><a>Home</a></li>
        <br />
        <li><a>About</a></li>
        <br />
        <li><a>My Work</a></li>
        <br />
        <li><a>Blog</a></li>
        <br />
        <li><a>Contact</a></li>
    </ul>
    <section id="coming-soon">
        <h2>Coming Soon!</h2>
    </section>
</nav>

示例如下:http://jsfiddle.net/gcJuz/1/

Oh awesome, classic example! The problem is that the jQuery thinks you've moused-out once you touch the section#coming-soon, so it obviously runs the fadeOut...

One way to overcome this is to put the section#coming-soon inside the nav, that way the script will still think you're inside the section#coming-soon, even if you hover over it:

<nav>
    <ul>
        <li><a>Home</a></li>
        <br />
        <li><a>About</a></li>
        <br />
        <li><a>My Work</a></li>
        <br />
        <li><a>Blog</a></li>
        <br />
        <li><a>Contact</a></li>
    </ul>
    <section id="coming-soon">
        <h2>Coming Soon!</h2>
    </section>
</nav>

Example here: http://jsfiddle.net/gcJuz/1/

善良天后 2025-01-04 01:51:07

只需提供一些建议即可提供帮助。

// cache the jquery object
 var target =  $('section#coming-soon');
 target.hide();
 // select the specific nav, not all navs
 $('nav:first').mouseenter(function(){
    // only need to show it once on enter
    target.show();
 }).mousemove(function(e) {
    // can't really avoid this unless you want to keep moving it around all the time
    // but show it only once you mouse over
    target.css({
        'position' : 'absolute',
         'top' : e.pageY,
         'left' : e.pageX    
     });
 }).mouseleave(function() {
    target.fadeOut(1300);
  });

Just a few recommendations to help out.

// cache the jquery object
 var target =  $('section#coming-soon');
 target.hide();
 // select the specific nav, not all navs
 $('nav:first').mouseenter(function(){
    // only need to show it once on enter
    target.show();
 }).mousemove(function(e) {
    // can't really avoid this unless you want to keep moving it around all the time
    // but show it only once you mouse over
    target.css({
        'position' : 'absolute',
         'top' : e.pageY,
         'left' : e.pageX    
     });
 }).mouseleave(function() {
    target.fadeOut(1300);
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文