2012-03-29 34 views
6

重要,請參見下面的jQuery的:如何使用jQuery的動畫()函數

我真的想知道我怎麼可以在這個代碼中使用CSS !importantheight

 $j('#lveis-wrapper_3').animate({ 
      opacity: 0.0, 
      height : 200 
     }, 1200); 

#lveis-wrapper_3的CSS低於:

#lveis-wrapper_3 
{ 
    width: 844px !important; 
    height: 936px !important; 
    margin: 0pt 0pt 10px !important; 
    padding: 0pt !important; 
    float: none; 
} 

我無法從height: 936px出於某些原因刪除!important ...
所以我想改變這種height通過animate() jQuery的功能 - 但如何?

+3

唉,所有這些'important' ......一定有辦法重新組織你的代碼... – elclanrs 2012-03-29 18:32:10

+0

@! elclanrs感謝評論:) – MoonLight 2012-03-29 18:41:05

回答

6

你只是不能這樣做。

按照 ...

「所有動畫屬性應該爲動感的單一數值 ......大多數屬性是非數字不能使用基本的jQuery進行動畫功能...」

http://api.jquery.com/animate/


我強烈建議重新檢查您對!important的需求。

+2

有時'!important'是有用的。我不擁有HTML代碼,也不能請求更改。這意味着我將不得不更換他們的CSS等,這使得代碼不必要的更長。 – Laoujin 2013-08-02 19:13:05

+2

@拉奧金,平心而論,我沒有說_「不要用它」_或_「不要用它」_。我只是說它的「需要」應該「重新審視」。 – Sparky 2013-08-02 19:18:03

0

你不能這樣做,但試試這個!

取決於它是否是DIV,IMG,...你將不得不做這樣的事情。此示例適用於圖像:

$j('img#lveis-wrapper_3').animate({ 
     opacity: 0.0, 
     height : 200 
    }, 1200); 

優先級在具有選擇器特性的CSS中定義。爲您選擇特異性#ID1.0.0,但加入IMG#ID現在1.0.1,因此優先級越高。

+1

很確定樣式表中的'!important'會覆蓋那個,不是嗎? – 2014-12-31 18:50:55

+0

@PlatinumAzure正確,只是測試它。 – superphonic 2016-12-01 17:37:54

2

您可以使用css轉換使其工作,因爲即使style屬性被$(elem).attr('style','...')更改,css轉換也可以工作。

你使用:

JS

// the element you want to animate 
$(elem).attr('style', 'your_style_with_important'); 

CSS

elem { 
    transition: all 0.2 linear; 
    -moz-transition: all 0.2 linear; 
    -webkit-transition: all 0.2 linear; 
}