如何使用!在jQuery animate()函数中很重要

Sil*_*ght 6 css jquery jquery-animate

请参阅下面的jQuery:

我真的不知道我该如何使用CSS !importantheight在此代码.

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

CSS #lveis-wrapper_3如下:

#lveis-wrapper_3
{
    width: 844px !important;
    height: 936px !important;
    margin: 0pt 0pt 10px !important;
    padding: 0pt !important;
    float: none;
}
Run Code Online (Sandbox Code Playgroud)

我无法!importantheight: 936px某些原因中删除...
所以我想height通过animate()jQuery函数改变它- 但是如何?

Spa*_*rky 6

你不能这样做.

根据jQuery文档 ...

"所有动画属性都应该动画为单个数值 ... ...大多数非数字属性无法使用基本的jQuery功能进行动画处理......"

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


我强烈建议您重新检查一下您的需求!important.

  • 有时`!important`很有用.我不拥有HTML代码,也不能请求更改.这意味着我将不得不替换他们的CSS等,这使代码不必要地更长. (2认同)
  • @Laoujin,公平地说,我没有说_"从不使用它"_或_"不要使用它"_.我只是说它的"需要"_应该"重新审视"_. (2认同)

小智 6

您可以使用 css 过渡来使其工作,因为即使通过 $(elem).attr('style', '...'); 更改了 style 属性,CSS 过渡也会工作。

你用:

js

// the element you want to animate
$(elem).attr('style', 'your_style_with_important');
Run Code Online (Sandbox Code Playgroud)

CSS

elem {
    transition: all 0.2 linear;
    -moz-transition: all 0.2 linear;
    -webkit-transition: all 0.2 linear;
}
Run Code Online (Sandbox Code Playgroud)