2011-01-29 40 views
1
var oldHeight = $("#outerCart").height; 
     $('#outerCart').animate(
        { height: (oldHeight + 100) }, { 
         duration: 'slow', 
         easing: 'easeOutBounce' 
        }); 

此時此代碼不起作用(罪魁禍首即將推出將oldHeight添加到新的高度)。把這樣一個常數將工作如何在jQuery中獲取元素的高度並在動畫中使用它

var oldHeight = $("#outerCart").height; 
     $('#outerCart').animate(
        { height: 200 }, { 
         duration: 'slow', 
         easing: 'easeOutBounce' 
        }); 

我如何使這種動態。 在此先感謝

+0

我不知道你的包裹 「oldHeight」 與parseInt函數會有所作爲。這樣你就可以確保你添加了整數而不是字符串加上一個整數,這將會失敗! – jnkrois 2011-01-29 03:25:10

+0

我試過了,但沒有奏效。感謝 – 2011-01-29 03:31:32

回答

1

你可以做"+=100"代替。

$('#outerCart').animate(
{ height: '+=100' }, { 
    duration: 'slow', 
    easing: 'easeOutBounce' 
}); 

從文檔的animate()(docs)方法:

動畫屬性也可以是相對的。如果一個值與領先+ =或供給 - =字符序列,則目標值通過加上或減去從屬性的當前值的給定數量的計算。

你的代碼沒有工作的原因是你沒有調用它的引用函數。

$("#outerCart").height; 

應該是:

$("#outerCart").height(); 
相關問題