【问题标题】:jquery animation headachejquery动画头痛
【发布时间】:2010-04-01 15:40:02
【问题描述】:

做一些 jquery 动画。我用 ani_seq='x' 的属性设置了某些 div,其中 x 为 1 到 9,然后分配了一个名为 'animate' 的类。我正在使用以下代码,它工作正常,并按顺序淡入每个项目:

    function my_animate( in_wrapper_id ) {
        $j("#" + in_wrapper_id + " .animate").hide(); // hide all items to be animated

        // animate all seq1 items --
        $j("#" + in_wrapper_id + " [ani_seq='1']").fadeIn( 1000, 
            function() { $j("#" + in_wrapper_id + " [ani_seq='2']").fadeIn( 1000,
                function() { $j("#" + in_wrapper_id + " [ani_seq='3']").fadeIn( 1000,
                    function() { $j("#" + in_wrapper_id + " [ani_seq='4']").fadeIn( 1000,
                        function() { $j("#" + in_wrapper_id + " [ani_seq='5']").fadeIn( 1000,
                            function() { $j("#" + in_wrapper_id + " [ani_seq='6']").fadeIn( 1000,
                                function() { $j("#" + in_wrapper_id + " [ani_seq='7']").fadeIn( 1000,
                                    function() { $j("#" + in_wrapper_id + " [ani_seq='8']").fadeIn( 1000,
                                        function() { $j("#" + in_wrapper_id + " [ani_seq='9']").fadeIn( 1000 ); });
                                    });
                                });
                            });
                        });
                    });
                });
            }); 
    }

我的问题是,有些项目不只是淡入。有些应该从左侧或右侧滑入。所以,我当然可以编写一个自定义函数来做到这一点。我不知道如何设置自定义函数,使其像 fadeIn() 函数一样工作,因为它接受将在动画完成时执行的回调函数。

例如,假设我有这样的函数(不确定这是正确的格式):

function custom_animate ( in_time_in_ms, in_callback_function ) {
    // get class of element, and based on class perform either
    // a fade-in, or a slide-in from left, or a slide-in from right
    // then after animation is done, return control back to calling
    // function so it can resume
}

我想用 custom_animate() 替换第一段代码中的所有 fadeIn() 调用,然后在该函数中,确定哪种要执行的动画。

谁能帮忙?

谢谢-

【问题讨论】:

  • 只是一个简短的说明 - 我将 $ 别名为 $j 因为我已经有一个我们使用的自定义 $ 函数。

标签: jquery queue effects


【解决方案1】:

只需将 in_callback_function 作为回调参数传递给您最终在 custom_animate() 中调用的任何动画函数。

$(something).fadeIn(1000, in_callback_function);

此外,您当前的代码实际上可以使用计数器变量或其他东西来代替重复的行;重复内容不是你的工作,而是计算机的工作。

这是一个未经测试的示例,应该可以帮助您进行(修正任何错别字留给读者作为练习):

function my_animate(wrapper_id) {
    var wrapper = $("#" + wrapper_id);

    wrapper.find(".animate").hide();
    // use data() to store the next index in the wrapper itself
    wrapper.data("ani_seq", 1);

    my_animate_run(wrapper);
}

function my_animate_run(wrapper) {
    var seq = wrapper.data("ani_seq");

    var el = wrapper.find("[ani_seq='" + seq + "']");
    if (!el.length) // reached the last element
        return;

    wrapper.data("ani_seq", seq + 1); // update the sequence
    var next = function() { my_animate_run(wrapper); };

    // choose your type of animation here
    el.fadeIn(1000, next);
}

【讨论】:

  • 我喜欢这个概念——你能提供一个简单的例子吗?
  • 为我的目的做了一些细微的修改,效果很好!非常感谢。
猜你喜欢
  • 2010-11-08
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-31
  • 2012-09-07
  • 2015-09-18
相关资源
最近更新 更多