jquery悬停问题
当用户将鼠标悬停在导航菜单上的按钮(#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过在没有回调的情况下执行此操作?
另一个选择是新的 jQuery 函数
.clearQueue()
Have you tried doing it without the callbacks?
Another option is the new jQuery function
.clearQueue()
尝试使用 .stop() 方法来中断之前的缓动
try to use the .stop() method to interrupt the previous easing