首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不带插件的pulse jquery

不带插件的pulse jquery
EN

Stack Overflow用户
提问于 2012-02-24 07:01:07
回答 1查看 4.6K关注 0票数 2

我有一个简单的淡入淡入,我想无限期地脉冲式进出。我已经找到了可以做到这一点的插件,但我很好奇jquery是否已经有了loop() api,这样我就可以在脚本中处理它了。

代码语言:javascript
复制
<script type="text/javascript">
$(document).ready(function(){    
    $('.bottom-left').delay(1000).fadeIn(900);
    $('.bottom-right').delay(3000).fadeIn(700);
});
</script>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-24 07:06:30

如果你想让它变得复杂,那么这可能会变成很多代码,但一个简单的实现只需要几行代码。基本上,您希望递归调用一个函数,该函数隐藏或显示动画函数的回调函数中的元素:

代码语言:javascript
复制
$(function () {

    //declare a function that can fade in/out any element with a specified delay and duration
    function run_animation($element, delay, duration) {

        //animate fade in/out after delay
        $element.delay(delay).fadeToggle(duration, function () {

            //after fade in/out is done, recursively call this function again with the same information
            //(if faded-out this time then next-time it will be faded-in)
            run_animation($element, delay, duration);
        });
    }

    //initialize the animations for each element, specifying a delay and duration as well
    run_animation($('.bottom-left'), 1000, 900);
    run_animation($('.bottom-right'), 3000, 700);
});

这是一个演示:http://jsfiddle.net/xpw4D/

.fadeToggle()文档:http://api.jquery.com/fadeToggle

更新

您可以对此代码进行一些增强,并像下面这样为两个以上的步骤添加动画:

代码语言:javascript
复制
$(function () {

    function run_animation(options) {

        //initialize the count variable if this is the first time running and reset it to zero if there are no more steps
        if (typeof options.count == 'undefined' || options.count >= options.steps.length) {
            options.count = 0;
        }

        options.element.delay(options.steps[options.count].delay).fadeToggle(options.steps[options.count].duration, function () {

            options.count++;

            run_animation(options);
        });
    }

    run_animation({
        element  : $('.bottom-left'),
        steps    : [
            { delay : 1000, duration : 100 },
            { delay : 500, duration : 900 },
            { delay : 3000, duration : 500 }
        ]
    });
    run_animation({
        element  : $('.bottom-right'),
        steps    : [
            { delay : 2000, duration : 200 },
            { delay : 1000, duration : 1800 },
            { delay : 6000, duration : 1000 }
        ]
    });
});​

这是一个演示:http://jsfiddle.net/jasper/xpw4D/2/

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9422818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档