开发者

.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,


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜