Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variables to JQuery animate

$(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?

like image 749
Eslam A. Hefnawy Avatar asked Feb 23 '26 17:02

Eslam A. Hefnawy


1 Answers

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.

like image 171
adeneo Avatar answered Feb 26 '26 06:02

adeneo