3
$(document).ready(function(){
for(var i=1;i<5;i++){
    var pos = -411*i;
    var pospx = "{'background-position-x':'"+ pos.toString() + "px'}";
    $("#newsPix").delay(2000).animate(pospx, 1000);
    }
});

I'm a jquery beginner, and I'm trying to use animated sprites to make something similar to a slideshow. I've been trying to make this code work for hours but I'm not sure where the probelm is! I've checked the HTML and CSS and they seem fine. I think the problem lies in either passing a value to the animate method OR in string adding for the pospx variable. any ideas?

1
  • Can you make a Fiddle that reflects your problem? Commented Feb 4, 2013 at 18:32

1 Answer 1

8

You're passing a string, an object would be more appropriate as that is what animate() accepts :

$(document).ready(function(){
    for(var i=1; i<5; i++){
        var pos   = -411*i,
            pospx = {'background-position-x' : pos};

        $("#newsPix").delay(2000).animate(pospx, 1000);
    }
});

And background-position-x is not supported in all browsers.

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

2 Comments

@EslamA.Hefnawy - no problem, you'll get more priveliges as you participate and post a few more questions / answers, and then it's a lot more fun.
in my case I follow the given code but not work if I set the value manually then it's working fine ` for(var i = from; i<4; i++){ var p = $("#tab"+i).css('right'); if(p){ p = 300;//parseInt(p.replace('px','')); } //alert(position); position = {right: p}; $("#tab"+i).delay(2000).animate(position).animate(pospx, 1000); }`

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.