jon
jon

Reputation: 35

.animate() not working?

can't make my background image' position animate

$(function() {
$('#nav1').bind('click',function(event){
        $('ul.nav').stop().animate({backgroundPosition: 'right top'}, 1000); 
});
    $(function() {
    $('#nav2').bind('click',function(event){
            $('ul.nav').stop().animate({backgroundPosition: 'right 38px'}, 1000); 
});
    $(function() {
    $('#nav3').bind('click',function(event){
            $('ul.nav').stop().animate({backgroundPosition: 'right 76px'}, 1000); 
});
    $(function() {
    $('#nav4').bind('click',function(event){
            $('ul.nav').stop().animate({backgroundPosition: 'right 114px'}, 1000); 
});

and html code is

<ul class="nav">
<li><a href="#what"  id="nav1">what</a></li>
<li><a href="#who" id="nav2">who</a></li>
<li><a href="#portfolio" id="nav3">portfolio</a></li>
<li><a href="#contact" id="nav4">contact</a></li>
</ul>

i've tried not using pixels like this {backgroundPosition: '0 38'} but it still not animating it just changed the position

and there is another issue. i repeat the jquery code for each item in the menu , could you plz make it in one function ,the position shifts 38px vertically for each item. thanks,

Upvotes: 3

Views: 676

Answers (3)

attack
attack

Reputation: 1523

It's not you.

jQuery doesn't natively support background position animations. However, there is a wonderful plugin: http://www.protofunc.com/scripts/jquery/backgroundPosition/

And here's the function you asked for:

$(function() {
    $('ul.nav a').each(function(i, elem) {
        $(elem).bind('click', function(event) {
            event.preventDefault();
            var offset = i * 38;
            $(this).stop().animate({backgroundPosition: '0 ' + offset + 'px'}, 1000); 
        });
    });
});

Upvotes: 2

c-smile
c-smile

Reputation: 27460

'right 38px' is not valid value of background-position. Fix it and it should work.

Upvotes: 0

JacobTheDev
JacobTheDev

Reputation: 18510

I'd recommend using CSS3 transitions and jQuery.CSS() to trigger the transition.

Upvotes: 0

Related Questions