简体   繁体   English

用动画删除CSS类

[英]Remove css class with animation

I'm creating a table and i want to highlight a specific row. 我正在创建一个表格,我想突出显示特定的一行。

I did this using: 我这样做是使用:

$this.css('background-color', 'green');

$this.delay(3000).animate({ backgroundColor: $color }, 3000);

$this = the row in question.

$color = the previous row color.

But i want it to work with the a css class, so something like this 但是我希望它与css类一起工作,所以像这样

$this.addClass('highlight');

The class .highlight will only have a background-color . .highlight类仅具有background-color

The problems is that, after i add the class, i can't the background-color . 问题是,在我添加了类之后,我无法使用background-color

If i use: 如果我使用:

$this.delay(3000).animate({ backgroundColor: $color }, 3000);

it doesn't seem to work because it doesn't overrides the background-color property of the class .highlight itself. 它似乎不起作用,因为它没有覆盖.highlight类本身的background-color属性。 And i don't see a way to animate a removeClass method or even a switchClass from .highlight to '' . 而且我不明白的方式来动态显示removeClass方法甚至switchClass.highlight''

Is there any solution i'm not thinking off to do this. 有什么解决方案我不打算这样做。

Thanks in advance. 提前致谢。

Use CSS transitions instead. 请改用CSS过渡。 Better performance and simpler. 性能更好,更简单。

Fiddle example 小提琴的例子

transition:background-color 0.3s linear;

though this doesn't provide as much browser support for the animation, obviously. 尽管显然,这并没有为动画提供太多的浏览器支持。

You could use jQuery UI's .switchClass which animates all the style changes: .switchClass 您可以使用jQuery UI的.switchClass动画所有样式更改: .switchClass

Once completed highlighting, use the callback to switch it back. 突出显示完成后,使用回调将其切换回原来的状态。

$('div').click(function() {
    $(this).switchClass( "normal", "color", 1000, "easeInOutQuad", function(){
        $(this).delay(3000).switchClass( "color", "normal", 1000, "easeInOutQuad" );
});

}); });

Fiddle me here! 在这里摆弄我!

the .animate() function works with "numeric" properties like: height, width, left, etc.. but not with background-color. .animate()函数可使用“数字”属性,例如:高度,宽度,左侧等。但不适用于背景色。

You can try this: 您可以尝试以下方法:

$(document).ready( function() {
$('tr.normal').on('click', function() {
   $(this)
   .hide()
   .delay(3000)
   .fadeIn('slow')
   .toggleClass('highlight');
   });             
 });

You could use jQuery's addClass and removeClass, consider: 您可以使用jQuery的addClass和removeClass,请考虑:

if($(document).scrollTop() > 250)
{    
$('#div').addClass("show");
}
else
{
$('#div').removeClass("show");
}
});

What this is doing is replacing the original class, such as "hide" with the div class "show", this particular snippet of code displays a banner when the user scrolls 250px down the page. 这样做是用div类“ show”替换原始类,例如“ hide”,当用户向下滚动页面250px时,此特定代码段显示标题。

Remember if you're using this code that it's still better (and smoother) to use CSS3 transitions UNLESS you're considering users who's browsers don't support this, such as IE8-. 请记住,如果您使用的是这段代码,那么使用CSS3过渡效果更好(更流畅),除非您考虑的是浏览器用户不支持此功能,例如IE8-。

EDIT: I just realized the reason you're doing it this way is because you're considering IE7 users. 编辑:我刚刚意识到您这样做的原因是因为您正在考虑IE7用户。 Perfect. 完善。 I have literally just solved this issue myself. 我实际上只是自己解决了这个问题。

The workaround I used was to have a css3 transition set up, and a detector with an if statement to use jQuery where transition isn't supported, see below: 我使用的解决方法是设置一个css3过渡,以及一个带有if语句的检测器,以使用不支持过渡的jQuery,请参见下文:

var Detect = (function() {
            var
            //Add CSS properties to test for
                            props = "transition".split(","),
            //Browser prefixes
                            CSSprefix = "Webkit,Moz,O,ms,Khtml".split(","),
                            d = document.createElement("detect"),
                            test = [],
                            p, pty;
            // test prefixed code
            function TestPrefixes(prop) {
                            var
                                            Uprop = prop.charAt(0).toUpperCase() + prop.substr(1),
                                            All = (prop + ' ' + CSSprefix.join(Uprop + ' ') + Uprop).split(' ');
                            for (var n = 0, np = All.length; n < np; n++) {
                                            if (d.style[All[n]] === "") return true;
                            }
    return false;
            }
            for (p in props) {
                            pty = props[p];
                            test[pty] = TestPrefixes(pty);
            }
            return test;

            }());


if (Detect.transition) {
        $(function(){
$(window).scroll(function() {
//your code here
//remember to use an if else

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM