将自定义动画类添加到jQuery

时间:2017-07-03 14:09:08

标签: jquery css-animations animate.css

我在CSS中创建了一个自定义动画类,我使用jQuery动态添加到元素中。一旦animate.css动画完成,我的自定义动画类就会开始。

我遇到的问题是我的自定义动画没有播放,而对于我的生活,我无法弄清楚我做错了什么。我可以在开发工具中看到该类正被添加到我的元素但动画没有发生。

custom-fade css:

.custom-fade{
    -webkit-animation: 3s ease 0s normal forwards 1 custom;
    animation: 3s ease 0s normal forwards 1 custom;
}

@keyframes custom{
    0% { opacity:.5; }
    66% { opacity:.2; }
    100% { opacity:0; }
}

@-webkit-keyframes custom{
    0% { opacity: .5; }
    66% { opacity: .2; }
    100% { opacity: 0; }
}

@-moz-keyframes custom{
    0% { opacity: .5; }
    66% { opacity: .2; }
    100% { opacity: 0; }
}

jQuery的:

var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
var fadeInUp = 'animated fadeIn fadeInUp';
var fadeOut = 'animated fadeOut';
var customFade = '.custom-fade';

$('.bg-img').addClass(fadeInUp);

    $('.bg-img').one( animationEnd, function(){
        $(this).removeClass(fadeInUp).addClass('.custom-fade');
    });

1 个答案:

答案 0 :(得分:1)

问题在于添加课程。您应该在'custom-fade'中提供'.custom-fade'而不是addClass()

示例代码



jQuery:

var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
var fadeInUp = 'animated fadeIn fadeInUp';
var fadeOut = 'animated fadeOut';
var customFade = '.custom-fade';

$('.bg-img').addClass(fadeInUp);

    $('.bg-img').on( animationEnd, function(){
        $(this).removeClass(fadeInUp).addClass('custom-fade');
    });

.custom-fade{
    -webkit-animation: 3s ease 0s normal forwards 1 custom;
    animation: 3s ease 0s normal forwards 1 custom;
}

@keyframes custom{
    0% { opacity:.5; }
    66% { opacity:.2; }
    100% { opacity:0; }
}

@-webkit-keyframes custom{
    0% { opacity: .5; }
    66% { opacity: .2; }
    100% { opacity: 0; }
}

@-moz-keyframes custom{
    0% { opacity: .5; }
    66% { opacity: .2; }
    100% { opacity: 0; }
}
.bg-img{
width:200px;
height:500px;
background:red;
}

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bg-img"></div>
&#13;
&#13;
&#13;