tags:

views:

65

answers:

1

There's something wrong with this code, or the way I've laid out my menu because animating the background position won't work. In my CSS I've simply assigned every a in the div.nav to have a background with 0 0 positioning as well as display: block and a specific width + height. What happens is image switch is done instantly instead of scrolling in (-95px on background positioning). What am I missing?

$('div.nav a').mouseover(function () {
    if (!$(this).is('.active')) {
        $(this).stop().animate({
            backgroundPosition: '0 0'
        }, {
            duration: 500
        });
    }
});

$('div.nav a').mouseout(function () {
    if (!$(this).is('.active')) {
        $(this).stop().animate({
            backgroundPosition: '0 0'
        }, {
            duration: 500
        });
    }
});
A: 

jQuery, natively, can't handle that properly because it needs two values to modify.

http://snook.ca/archives/javascript/jquery-bg-image-animations/

You should replace backgroundPosition: '0 0' with backgroundPosition: '0px 0px'

That should work, but it should also just animate the x-axis.

jAndy
That's exactly the tutorial I began working with (I haven't used background positioning with animate before) but the end result didn't work - even if I copied the authors' code directly on to my elements. I guess one way could be to have a regular image inside the a element then use it's background property to "extend" how that image looks by another image (which is what I'm trying to achieve). Just seems unnecessary.
Staffan Estberg
Aw, you know what. I missed the tiny, additional plug-in the author had created... Working perfectly now! ;)Too bad this function requires extra code though, should be a core feature.
Staffan Estberg
@Staffan: feel free to add your suggestion to http://forum.jquery.com/
jAndy