2013-01-13 28 views
2

我想知道是否可以根據某些條件在jQuery的animate中設置屬性。例如:動畫/任何函數內的條件語句

var condition = offL > ((wW/2) - $this.width()); 
    tooltip.css(function() { 
     if (condition) { 
     return '"right", "auto"'; 
     } else { 
     return '"left", "auto"'; 
     } 
    }).animate({ 
     "top": $this.offset().top + (posT/2) - (tooltip.height()/2), 
     if (condition) { 
     return '"left":"offL - tooltip.width() - 30"'; 
     } else { 
     return '"right", "offR - tooltip.width() - 30"'; 
     } 
    }, 300).fadeTo(200, 1); 

tooltip等瓦爾較早定義。)

這不起作用,很明顯。但是,如果他需要在函數內部使用條件參數,會怎麼樣呢?

另一方面的問題是,我的使用情況爲var正確嗎?

+0

你不這樣做,你打造出一個變量來保存你所需要的數據。 – Ohgodwhy

回答

3

在動畫前簡單做出決定。 爲exampaple:

var properties = {} 
if(condtion) { 
    properties = {left: someThing}; 
else 
    properties = {prop1: value1, prop2: value2}; 
$(obj).animate(properties,'fast'); 

,是的,你的條件使用是正確的。因爲它的一個條件

var condition = offL > ((wW/2) - $this.width()); 

將被評估爲true或false;)