【问题标题】:jQuery repeat animations within functionsjQuery在函数内重复动画
【发布时间】:2010-12-02 00:27:16
【问题描述】:

我有一个简单的 Flash 动画,我正在尝试用 jQuery 重建它。我有 5 个动画被链接起来一个接一个地开始。在所有动画触发后,我需要它暂停 5 秒,自行清除并重新开始动画。我需要无限期地这样做。 这是我的代码:

$(".bars").ready(function() {
    $(".barone").animate({
        'height': '49px'
    }, 2000, function() {
        $(".bartwo").animate({
            'height': '112px'
        }, 2000, function() {
            $(".barthree").animate({
                'height': '174px'
            }, 2000, function() {
                $(".barfour").animate({
                    'height': '236px'
                }, 2000, function() {
                    $(".barfive").animate({
                        'height': '298px'
                    }, 2000, function() {
                        $('.bars div').delay(5000, function() {
                            $('.bars div').css({
                                "height": "0"
                            });
                        });
                    });
                });
            });
        });
    });
});

请原谅我的源格式,此文本字段与我不一致。

您还可以在以下位置查看示例:http://execusite.com/fazio/files/careers-banner/test.html

非常感谢任何帮助

那么这会是正确的吗?

函数 animateThis(){ $(".bars div:nth-child(1)").animate({'height':'49px'}, 2000, function() { $(".bars div:nth-child(2)").animate({'height':'112px'}, 2000, function() { $(".bars div:nth-child(3)").animate({'height':'174px'}, 2000, function() { $(".bars div:nth-child(4)").animate({'height':'236px'}, 2000, function() { $(".bars div:nth-child(5)").animate({'height':'298px'}, 2000, function() {
$('.bars div').delay(2000).css({"height" : "0"}), function() { animateThis()}; }); }); });
}); }); } $(".bars").ready(function(){animateThis()});

我为我的无知道歉,我对 jQuery 有点陌生,但仍在努力掌握语法。

【问题讨论】:

  • 在编辑帖子时,您可以选择一个区域,然后点击“1010”小按钮“缩进4个空格”。
  • 您可以明确地改进代码,概括代码并从函数中调用它

标签: jquery


【解决方案1】:

将您的代码放在一个函数中。当最后一个动画发生时,再次触发该功能。使用链式 .delay(5000) 暂停 5 秒

sudo 代码:

function myAnimate(){
...your code.delay(5000).animate(....function(){ myAnimate() })
}

【讨论】:

  • @user406905,请见上文。
  • 是的,看起来不错。只需添加一些分号即可 100% 正确。
【解决方案2】:

我会将你的动画封装在一个函数中,然后使用 JavaScript 的 setInterval(func, interval)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多