2013-08-30 46 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

相關問題