1

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.

3
  • This is a duplicate (it comes up fairly regularly). For example (and an answer!) see: stackoverflow.com/questions/2009151/…. Commented Jan 20, 2010 at 21:38
  • Thanks - I did have a look around before I posted but didn't come across this. Commented Jan 20, 2010 at 22:34
  • in case anyone reads this: try to use .prop instead .attr ;-) Commented Dec 7, 2016 at 7:30

2 Answers 2

1

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()

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the advise, but without the callbacks it changes the image before it does the first fadeOut. Solved now using .stop(), and .clearQueue() also seems to work. The jquery website says .stop() is meant to be only for animations whereas .clearQueue() is more generic, so seeing as I've got animations and the source change, would .clearQueue() be 'more' correct?
There's no real 'more' correct here. It depends on what you want to do. stop() will halt the current animation. clearQueue() and stop(true, true) will do the same thing and remove all elements from the queue. 'Tis more a matter of preference, so if you've got it using stop, just keep it with stop.
0

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);
});

1 Comment

Thanks - .stop() seems to do the trick, but to get the order right the source change and fadeIn still need to be in the callback function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.