2012-02-10 37 views
18

我無法弄清楚用jQuery動畫marginLeft。我需要它減去938px每次用戶點擊一個鏈接,這是工作正常,當我使用.css(),但我不知道如何使它與.animate()工作。使用jQuery動畫marginLeft

$("#full-wrapper #full").animate({ 
    marginLeft, -=938px 
}, 500); 

任何人都可以找出爲什麼這不起作用?這是我的CSS版本:

$("#full-wrapper #full").css("marginLeft","-=938px"); 

我在使用CSS3進行動畫製作,但需要使它在舊版瀏覽器中工作。

回答

37

您的代碼中存在語法錯誤,因爲您正在將對象中的參數傳遞到animate(),因此應該使用:而不是,來分隔每個屬性。試試這個:

$("#full-wrapper #full").animate({ 
    marginLeft: '-=938px' 
}, 500); 

Example fiddle

+3

你打我吧! – peduarte 2012-02-10 15:14:45

+0

這也行不通,你錯過了報價。 – ShankarSangoli 2012-02-10 15:15:09

+1

工作正常。謝謝,這是一個noob錯誤哈哈。 – JacobTheDev 2012-02-10 15:35:53

1
$("#full-wrapper #full").animate({ 
    marginLeft: '-=938px' 
}, 500); 
2

更換由冒號(:) comman(,)。

$("#full-wrapper #full").animate({ 
    marginLeft: "-=938px" 
}, 500);