代码之家  ›  专栏  ›  技术社区  ›  TJB

jQuery:淡出-做某事-淡入模式

  •  3
  • TJB  · 技术社区  · 14 年前

    我似乎经常编写类似以下模式的jQuery代码:

    淡出==>在幕后做点什么==>淡入
    图示如下:

    /// <reference path="jquery-1.4.2.js" />
    /// <reference path="jquery-1.4.2-vsdoc.js" />
    /// <reference path="jquery.validate-vsdoc.js" />
    var fade = "slow";
    
    $(document).ready(function () {
    
        // Some event occurs
        $("#Trigger").change(function () {
            var id = $(this).find(":selected").val();        
    
            // Fade out target while I do something
            $("#Target").fadeOut(fade, function () {
                if (id != "") {
    
                    // Do Something
                    $("#Target").load(
                        "/Site/Controller/Action/"+id, null,
                        function () {
    
                            // Fade in Target
                            $("#Target").fadeIn(fade);
                        });
                }
            });
        });
    });
    

    这样做很好,但是回调层次结构变得非常深入

    2 回复  |  直到 14 年前
        1
  •  6
  •   Eric    14 年前

    jQuery's .queue

    $("#Target")
        .fadeOut()
        .queue(function() {
            if (id != "")
                // Do Something
                $(this).load(
                    "/Site/Controller/Action/"+id, null,
                    $(this).dequeue
                );
            else
                $(this).dequeue();
        })
        .fadeIn()
    
        2
  •  1
  •   Community CDub    7 年前

    使用jQuery.queue()可以附加多个命令以按顺序执行。 你如何使用它取决于你想做什么。这里有两个解决方案:

    $('#label')
        .fadeOut()                     //animate element
        .queue(function() {            //do something method1
            ...your code here...
            $(this).dequeue //dequeue the next item in the queue
        })
        .queue(function (next) {          //do something method2
            ...your code here...
            next(); //dequeue the next item in the queue
        })
        .delay(5000)                   //do lots more cool stuff.
        .show("slow")
        .animate({left:'+=200'},2000)
        .slideToggle(1000)
        .slideToggle("fast")
        .animate({left:'-=200'},1500)
        .hide("slow")
        .show(1200)
        .slideUp("normal", runIt)
        .fadeIn()
    

    var $header = $("#header");
    var $footer = $("#footer");
    
    function runIt() {
        $header.queue(function(next){
            ...do something...
            next();
            }
        $header.queue(function(next){
            functionABC(variable, next);
            })
        $footer.animate({left:'+=200'},2000);
        $("#left").slideToggle(1000);
        $(".class1").slideToggle("fast");
        $(".class2").animate({left:'-=200'},1500);
        $("whatever").delay(3000);
        $(".class3").hide("slow");
          }
    
        runIt();    //execute it now, or...
    
        $(window).load(function() {  //execute it after the page loads!
           runIt();
        })
    

    您也可以使用queue:false variable. 这意味着队列不会等待此操作完成,因此将立即开始下一个操作。一起制作两个动画很有用:

    function runIt() {
        $("#first").animate(
            {width: '200px'},
            {duration:2000, queue:false}
        );
        $footer.animate(
            {left:'+=200'},
            2000
        );
    }
    

    使用AJAX,队列会变得更加复杂。看看这个帖子:

    AJAX Queues on this post .