Passing variables to JQuery animate

我怕爱的太早我们不能终老 提交于 2019-12-10 19:06:25

问题


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


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.



来源:https://stackoverflow.com/questions/14693113/passing-variables-to-jquery-animate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!