jquery悬停问题

发布于 2024-08-18 17:59:27 字数 1603 浏览 3 评论 0原文

当用户将鼠标悬停在导航菜单上的按钮(#home、#events 等)上时,我尝试将网站的徽标(logoName.png)更改为其他图像(logoHome.png、logoEvents.png 等)下面的脚本。当时间设置为 0 并且一切都立即发生时,这种方法工作得很好,但是当我放慢速度时,它并没有按照我想要的方式进行。

如果用户在悬停动画完成之前将鼠标从按钮上移开,则不会恢复到徽标,并且如果用户直接在不同的导航按钮之间移动,则会显示徽标而不是按钮的导航图像鼠标现已打开。

我尝试过使用悬停、鼠标进入/离开和鼠标悬停/退出,但无济于事。有什么办法可以更好地处理队列吗?或者我应该尝试hoverIntent?

感谢所有建议 - 我对这一切都是新手!谢谢。

$('#home').hover(function() {
    $('#logo').hide(
        500,
        function() {
            $('#logo').attr ('src', 'images/logoHome.png').fadeIn(500); // swaps logo for home image
        });
}, function() {
    $('#logo').hide(
        500,
        function () {
            $('#logo').attr('src', 'images/logoName.png').fadeIn(500); //swaps back
        });
});

$('#events').hover(function() {
    $('#logo').hide(
        500,
        function() {
            $('#logo').attr ('src', 'images/logoEvents.png').fadeIn(500); //swaps logo for events image
        });
}, function() {
    $('#logo').hide(
        500,
        function () {
            $('#logo').attr('src', 'images/logoName.png').fadeIn(500); //swaps back
        });
});

编辑: - 任何感兴趣的人的工作版本:

$('#home').hover(function() {
    $('#logo').stop(true, true).fadeOut(
        500,
        function() {
            $('#logo').attr ('src', 'images/logoHome.png').fadeIn(500); // swaps logo for home image
        });
    }, function() {
$('#logo').stop(true, true).fadeOut(
    500,
    function () {
        $('#logo').attr('src', 'images/logoName.png').fadeIn(500); //swaps back
    });
});

感谢您的帮助。

I'm trying to change a website's logo (logoName.png) to other images (logoHome.png, logoEvents.png, etc.) when the user hovers over buttons on the navigation menu (#home, #events, etc.) with the script below. This works fine when the timings are set to 0 and everything happens instantaneously, but when I slow things down it's not doing what I want it to.

If the user moves the mouse away from the button before the hover animation is finished then it doesn't revert back to the logo, and if the user moves directly between different nav buttons, it shows the logo instead of the navigation image for the button the mouse is now on.

I've tried using hover, mouseenter/leave and mouseover/out to no avail. Is there any way of handling the queue better? Or should I give hoverIntent a try?

All suggestions appreciated - I'm new to all this! Thanks.

$('#home').hover(function() {
    $('#logo').hide(
        500,
        function() {
            $('#logo').attr ('src', 'images/logoHome.png').fadeIn(500); // swaps logo for home image
        });
}, function() {
    $('#logo').hide(
        500,
        function () {
            $('#logo').attr('src', 'images/logoName.png').fadeIn(500); //swaps back
        });
});

$('#events').hover(function() {
    $('#logo').hide(
        500,
        function() {
            $('#logo').attr ('src', 'images/logoEvents.png').fadeIn(500); //swaps logo for events image
        });
}, function() {
    $('#logo').hide(
        500,
        function () {
            $('#logo').attr('src', 'images/logoName.png').fadeIn(500); //swaps back
        });
});

EDIT: - Working version for anyone who's interested:

$('#home').hover(function() {
    $('#logo').stop(true, true).fadeOut(
        500,
        function() {
            $('#logo').attr ('src', 'images/logoHome.png').fadeIn(500); // swaps logo for home image
        });
    }, function() {
$('#logo').stop(true, true).fadeOut(
    500,
    function () {
        $('#logo').attr('src', 'images/logoName.png').fadeIn(500); //swaps back
    });
});

etc

Thanks for your help.

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

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

发布评论

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

评论(2

天涯离梦残月幽梦 2024-08-25 17:59:27

您是否尝试过在没有回调的情况下执行此操作?

$('#home').hover(function() {
    $('#logo').fadeOut(500).attr ('src', 'images/logoHome.png').fadeIn(500);
}, function() {
    $('#logo').fadeOut(500).attr('src', 'images/logoName.png').fadeIn(500);
});

$('#events').hover(function() {
    $('#logo').fadeOut(500).attr('src', 'images/logoEvents.png').fadeIn(500);
}, function() {
    $('#logo').fadeOut(500).attr('src', 'images/logoName.png').fadeIn(500);
});

另一个选择是新的 jQuery 函数 .clearQueue()

Have you tried doing it without the callbacks?

$('#home').hover(function() {
    $('#logo').fadeOut(500).attr ('src', 'images/logoHome.png').fadeIn(500);
}, function() {
    $('#logo').fadeOut(500).attr('src', 'images/logoName.png').fadeIn(500);
});

$('#events').hover(function() {
    $('#logo').fadeOut(500).attr('src', 'images/logoEvents.png').fadeIn(500);
}, function() {
    $('#logo').fadeOut(500).attr('src', 'images/logoName.png').fadeIn(500);
});

Another option is the new jQuery function .clearQueue()

£冰雨忧蓝° 2024-08-25 17:59:27

尝试使用 .stop() 方法来中断之前的缓动

$('#home').hover(function() {
    $('#logo').stop().fadeOut(500).attr ('src', 'images/logoHome.png').fadeIn(500);
}, function() {
    $('#logo').stop().fadeOut(500).attr('src', 'images/logoName.png').fadeIn(500);
});

$('#events').hover(function() {
    $('#logo').stop().fadeOut(500).attr('src', 'images/logoEvents.png').fadeIn(500);
}, function() {
    $('#logo').stop().fadeOut(500).attr('src', 'images/logoName.png').fadeIn(500);
});

try to use the .stop() method to interrupt the previous easing

$('#home').hover(function() {
    $('#logo').stop().fadeOut(500).attr ('src', 'images/logoHome.png').fadeIn(500);
}, function() {
    $('#logo').stop().fadeOut(500).attr('src', 'images/logoName.png').fadeIn(500);
});

$('#events').hover(function() {
    $('#logo').stop().fadeOut(500).attr('src', 'images/logoEvents.png').fadeIn(500);
}, function() {
    $('#logo').stop().fadeOut(500).attr('src', 'images/logoName.png').fadeIn(500);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文