0

作为一个例子,我以 jQuery animate 函数为例:正常它的工作原理是这样的:

$("#div1").animate({
                width: '300px',
            }, 1000, function() {
                console.log("animate 1 done");
            });

我想要这样:

animateConfig = [{
                width: '300px',

            }, 1000, function() {
                console.log("animate1 done");
            }];

startAnimate($("#div1"), animateConfig);

function startAnimate(obj, animateConfig) {
    console.log(animateConfig);
    obj.animate(animateConfig);

}

也是一个小提琴: http: //fiddle.jshell.net/hVXKM/1/

4

2 回答 2

3

尝试:

obj.animate.apply(obj, animateConfig);
于 2013-08-30T23:38:56.257 回答
1

.animate()方法允许您将对象作为第二个参数传递。这样你就可以这样做:

文档:.animate(属性,选项)

animateConfig = {
    props: { width: '300px' },
    options: {
        duration: 1000,
        complete: function() {
           console.log("animate1 done");
        }
    }
}

那么你可以这样做:

function startAnimate(obj, config) {
    obj.animate(config.props, config.options);
}

startAnimate(obj, animateConfig);
于 2013-08-30T23:43:29.320 回答