2012-03-29 71 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; 
}