【问题标题】:pulse jquery without a plugin没有插件的脉冲 jquery
【发布时间】:2012-02-23 23:01:07
【问题描述】:

我有一个简单的淡入淡出,我想无限期地进出。我找到了执行此操作的插件,但很好奇 jquery 是否已经有一个 loop() api,所以我可以在脚本中处理它。

<script type="text/javascript">
$(document).ready(function(){    
    $('.bottom-left').delay(1000).fadeIn(900);
    $('.bottom-right').delay(3000).fadeIn(700);
});
</script>

【问题讨论】:

    标签: jquery pulse throbber


    【解决方案1】:

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

    $(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

    更新

    您可以稍微加强一下这段代码,并像这样为两个以上的步骤设置动画:

    $(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/

    【讨论】:

    • 太棒了,谢谢!距离检查还有 2 分钟。再次感谢!
    • 您可以创建一个对象来存储动画的步骤,每个步骤将包括一个延迟、一个持续时间和一个跟踪动画所在步骤的计数器。请参阅我的代码的 更新 以获取此示例:jsfiddle.net/jasper/xpw4D/2。请注意,我没有编写 S.O.S.闪烁模式,但这是你这样做的一种方法...... :)
    • 再次很棒。如果有很棒的答案帖子的 stackoverflow 库,应该在其中。
    • 我认为这个问题是当元素淡出时,它实际上是从页面流中移除的......:/
    • @rednoyz 为我工作,您遇到什么问题以及哪个版本的 FF?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    相关资源
    最近更新 更多