代码之家  ›  专栏  ›  技术社区  ›  Elazar Leibovich

jquery同步动画

  •  20
  • Elazar Leibovich  · 技术社区  · 15 年前

    在许多情况下,我希望动画同步执行。尤其是当我想制作一系列连续动画时。

    有什么简单的方法可以进行jquery吗 animate 函数调用同步?

    我唯一想到的方法是在动画完成后设置一个标志为真,然后等待这个标志。

    7 回复  |  直到 10 年前
        1
  •  25
  •   SLaks    15 年前

    jquery无法进行同步动画。

    记住,javascript运行在浏览器的UI线程上。

    如果制作同步动画,浏览器将冻结,直到动画完成。

    你为什么要这么做?

    您可能应该使用jquery的回调参数并在回调中继续方法代码,如下所示:

    function doSomething() {
        var thingy = whatever;
        //Do things
        $('something').animate({ width: 70 }, function() {
            //jQuery will call this method after the animation finishes.
            //You can continue your code here.
            //You can even access variables from the outer function
            thingy = thingy.fiddle;
        });
    }
    

    这被称为终结。

        2
  •  6
  •   Alain BECKER    14 年前

    我想你应该看看jquery queue() 方法。

    queue()的doc explain jquery动画不仅不会真正阻塞用户界面,而且会逐个对其进行排队。

    它还提供了一种使动画和函数调用顺序化的方法(这是我对您所说的意思的最好理解” 同步的 “”,像:

    $("#myThrobber")
        .show("slow")                 // provide user feedback 
        .queue( myNotAnimatedMethod ) // do some heavy duty processing
        .hide("slow");                // provide user feedback (job's 
    
    myNotAnimatedMethod() { // or animated, just whatever you want anyhow...
        // do stuff
        // ...
    
        // tells #myThrobber's ("this") queue your method "returns", 
        // and the next method in the queue (the "hide" animation) can be processed
        $(this).dequeue();
    
        // do more stuff here that needs not be sequentially done *before* hide()
        // 
    }  
    

    当然,这对于异步处理来说是多余的;但是如果您的方法实际上是一个普通的老同步JavaScript方法,那么这可能就是实现它的方法。

    希望这对我有帮助,也为我糟糕的英语感到抱歉…

        3
  •  2
  •   shuckster    15 年前

    jquery为其.animate()方法提供了一个“step”回调。您可以使用此链接进行同步动画:

    jQuery('#blat').animate({
      // CSS to change
      height: '0px'
    },
    {
      duration: 2000,
      step: function _stepCallback(now,opts) {
        // Stop browser rounding errors for bounding DOM values (width, height, margin, etc.)
        now = opts.now = Math.round(now);
    
        // Manipulate the width/height of other elements as 'blat' is animated
        jQuery('#foo').css({height: now+'px'});
        jQuery('#bar').css({width: now+'px'});
      },
      complete: function _completeCallback() {
        // Do some other animations when finished...
      }
    }
    
        4
  •  1
  •   Corey Ballou    15 年前

    我同意@slaks的说法。您应该使用jquery对给定动画的回调来创建同步动画。基本上,您可以为当前动画获取任何内容,然后像这样将其拆分:

    $yourClass = $('.yourClass');
    $yourClass.animate({
        width: "70%"
    }, 'slow', null, function() {
        $yourClass.animate({
            opacity: 0.4
        }, 'slow', null, function() {
            $yourClass.animate({
                borderWidth: "10px"
            });
        });
    });
    
        5
  •  0
  •   Jason Miesionczek    13 年前

    这里有一个模块,我放在一起一段时间,以帮助运行动画顺序。

    用途:

    var seq = [
        { id: '#someelement', property:'opacity', initial: '0.0', value:'1.0', duration:500 },
        { id: '#somethingelse', property:'opacity', value:'1.0', duration: 500 }
    ];
    
    Sequencer.runSequence(seq);
    

    var Sequencer = (function($) {
        var _api = {},
            _seq = {},
            _seqCount = 0,
            _seqCallback = {};
    
        function doAnimation(count, step) {
            var data = _seq[count][step],
                props = {};
    
                props[data.property] = data.value
    
            $(data.id).animate(props, data.duration, function() {
                if (step+1 < _seq[count].length) {
                    doAnimation(count, ++step);
                } else {
                    if (typeof _seqCallback[count] === "function") {
                        _seqCallback[count]();
                    }
                }
            });
        }
    
        _api.buildSequence = function(id, property, initial, steps) {
            var newSeq = [],
                step = {
                    id: id,
                    property: property,
                    initial: initial
                };
    
            $.each(steps, function(idx, s) {
                step = {};
                if (idx == 0) {
                    step.initial = initial;
                }
                step.id = id;
                step.property = property;
                step.value = s.value;
                step.duration = s.duration;
                newSeq.push(step);
            });
    
            return newSeq;
        }
    
        _api.initSequence = function (seq) {
            $.each(seq, function(idx, s) {              
                if (s.initial !== undefined) {
                    var prop = {};
                    prop[s.property] = s.initial;
                    $(s.id).css(prop);
                }            
            });
        }
    
        _api.initSequences = function () {
            $.each(arguments, function(i, seq) {
                _api.initSequence(seq);
            });
        }
    
        _api.runSequence = function (seq, callback) {
            //if (typeof seq === "function") return;
            _seq[_seqCount] = [];
            _seqCallback[_seqCount] = callback;
    
            $.each(seq, function(idx, s) {
    
                _seq[_seqCount].push(s);
                if (s.initial !== undefined) {
                    var prop = {};
                    prop[s.property] = s.initial;
                    $(s.id).css(prop);
                }
    
            });
    
    
            doAnimation(_seqCount, 0);
            _seqCount += 1;
        }
    
        _api.runSequences = function() {
            var i = 0.
                args = arguments,
                runNext = function() {
                    if (i+1 < args.length) {
                        i++;
                        if (typeof args[i] === "function") {
                            args[i]();
                            runNext();
                        } else {
                            _api.runSequence(args[i], function() {
                                runNext();
                            });
                        }
                    }
                };
    
            // first we need to set the initial values of all sequences that specify them
            $.each(arguments, function(idx, seq) {
                if (typeof seq !== "function") {
                    $.each(seq, function(idx2, seq2) {
                        if (seq2.initial !== undefined) {
                            var prop = {};
                            prop[seq2.property] = seq2.initial;
                            $(seq2.id).css(prop);
                        }
                    });
                }
    
            });
    
            _api.runSequence(arguments[i], function (){
                runNext();
            });
    
        }
    
        return _api;
    }(jQuery));
    
        6
  •  0
  •   Mike    12 年前

    jquery可以制作同步动画。看看这个:

    function DoAnimations(){
      $(function(){
        $("#myDiv").stop().animate({ width: 70 }, 500);
        $("#myDiv2").stop().animate({ width: 100 }, 500);
      });
    }
    
        7
  •  0
  •   Hobo    10 年前

    我发现了这个 http://lab.gracecode.com/motion/ 非常容易使用,与jquery结合使用效果很好。

    编辑 链接似乎断了。如果我跟踪了那些回程档案,代码就在 https://github.com/feelinglucky/motion