jQuery如何“链接”条件/函数

时间:2010-12-16 17:01:56

标签: jquery

我有以下代码块:

if (current <= total) {

            $next.css({opacity: 0.0})
            .addClass("active")
            .animate({opacity: 1.0}, 1000, function() {
                current -= 1;
                if (current == 1)
                    {
                        jQuery("#next").animate({opacity: 0}, 500); 
                        next_isShowing = false;
                    }
                if (!prev_isShowing) 
                    {
                        jQuery("#prev").animate({opacity: 1.0}, 500); 
                    }


            });

唯一的问题是,我实际上想在“.animate”之前移动这段代码:

    current -= 1;
if (current == 1)
            {
                jQuery("#next").animate({opacity: 0}, 500); 
                next_isShowing = false;
            }

有没有办法获取这个文本块并将其“链接”到我当前的链中?

2 个答案:

答案 0 :(得分:1)

听起来你正在尝试写

$(...)
    .filter(function() { return current === 1; })
    .animate(...)
    .end()
    .thingy(...)

答案 1 :(得分:0)

在这种特殊情况下,由于减量和条件对另一个对象起作用,我可以将它移到链外:

if (current <= total) {

            $next.css({opacity: 0.0})
            .addClass("active")
            .animate({opacity: 1.0}, 1000, function() {
                if (!prev_isShowing) 
                    {
                        jQuery("#prev").animate({opacity: 1.0}, 500); 
                    }
            });

           current -= 1;
           if (current == 1)
              {
                jQuery("#next").animate({opacity: 0}, 500); 
                next_isShowing = false;
              }
}

但我发现这个插件以防任何人需要链接条件:

http://benalman.com/code/javascript/jquery/jquery.ba-cond.js

像: