jQuery显示div为2秒然后在循环中隐藏4秒

时间:2017-06-14 16:37:26

标签: javascript jquery

如何显示div 2秒,然后在无限循环中隐藏4秒?我使用jQuery的animate()函数,因为我也想使用CSS转换。

function animatedText() {
    setTimeout(function() {
        $('.text').animate({ opacity: 1 }, 200, function() {
            setTimeout(function() { 
                $('.text').animate({ opacity: 0 }, 200);
            }, 1800);
        });
    }, 3800);
}
setInterval(animatedText(), 6000);

这是我的小提琴:https://jsfiddle.net/od6gm8t3/

1 个答案:

答案 0 :(得分:1)

我希望这会对你有所帮助。请查看以下代码。

function animatedText() {
    $('.text').animate({ opacity: 1 }, 200, function() {
            setTimeout(function() { 
                $('.text').animate({ opacity: 0 }, 200);
            }, 2000);
    });
    setTimeout(function() {
        animatedText();
    },6000);
}
animatedText();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<i class="text">Animated Text</i>