将变量传递给JQuery动画 [英] Passing variables to JQuery animate

查看:75
本文介绍了将变量传递给JQuery动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我是一个jquery初学者,我正在尝试使用动画精灵来制作类似于幻灯片。我一直试图使这段代码工作几个小时,但我不确定问题出在哪里!我检查了HTML和CSS,看起来很好。我认为问题在于将值传递给animate方法或者为pospx变量添加字符串。任何想法?

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?

推荐答案

你传递一个字符串,一个对象会更合适,因为这是animate()接受的:

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

background-position-x 不是在所有浏览器中都受支持。

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

这篇关于将变量传递给JQuery动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆