2013-08-30 45 views
0

作为一个例子,我采取jQuery的动画功能: 正常它的工作原理是这样的:给几个参数到功能与1个变量/对象

$("#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/

回答

3

尝试:

obj.animate.apply(obj, animateConfig); 
+0

THX这一点。我读了关于适用现在 – nbar

1

.animate()方法可让您传递一个对象作为第二个参数。这样,你可以这样做:

文档:.animate(properties, options)

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); 
+0

thx这很好。 – nbar

相关问题