javascript - Stagger animation in a random order with TweenMax - Stack Overflow

I'm trying to create a staggerTo() animation with TweenMax that affects elements in a random order

I'm trying to create a staggerTo() animation with TweenMax that affects elements in a random order, meaning I don't want the actual animation to be random but its order.

For that I take all the elements I want to animate and shuffle using this function:

$.fn.shuffle = function() {

    var allElems = this.get(),
        getRandom = function(max) {
            return Math.floor(Math.random() * max);
        },
        shuffled = $.map(allElems, function(){
            var random = getRandom(allElems.length),
                randEl = $(allElems[random]).clone(true)[0];
                allElems.splice(random, 1);
                return randEl;
        });

    return $(shuffled);

};

var elements = $('.animate').shuffle();

I then lunch the staggerTo animation:

TweenMax.staggerTo(elements, 0.1, {y: 100, ease: Quad.easeInOut}, 0.1);

But of course, I then realized I had it all wrong, as TweenMax wasn't animating the actual DOM elements but rather its virtual clones.

Unfortunately, I don't know how to proceed from here.

Could anyone help me out? Thanks!

I'm trying to create a staggerTo() animation with TweenMax that affects elements in a random order, meaning I don't want the actual animation to be random but its order.

For that I take all the elements I want to animate and shuffle using this function:

$.fn.shuffle = function() {

    var allElems = this.get(),
        getRandom = function(max) {
            return Math.floor(Math.random() * max);
        },
        shuffled = $.map(allElems, function(){
            var random = getRandom(allElems.length),
                randEl = $(allElems[random]).clone(true)[0];
                allElems.splice(random, 1);
                return randEl;
        });

    return $(shuffled);

};

var elements = $('.animate').shuffle();

I then lunch the staggerTo animation:

TweenMax.staggerTo(elements, 0.1, {y: 100, ease: Quad.easeInOut}, 0.1);

But of course, I then realized I had it all wrong, as TweenMax wasn't animating the actual DOM elements but rather its virtual clones.

Unfortunately, I don't know how to proceed from here.

Could anyone help me out? Thanks!

Share Improve this question asked Apr 10, 2015 at 15:13 Stephanie D.Stephanie D. 1192 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

I am probably failing to understand your scenario but it seems that at the core of it, I think your problem is more about randomizing an array of jQuery elements. Have you taken a look at this?

  1. Convert your elements to array like so: var elements=$('.animate').toArray();.
  2. Shuffle this array using this technique: elements.sort(function(){return 0.5-Math.random()});. Thanks to CSS-Tricks..
  3. Then feed this new array to the staggerTo call: TweenMax.staggerTo(elements, 0.1, {y: 100, ease: Quad.easeInOut}, 0.1); i.e. what you already have.
  4. Quick jsFiddle here.

Please note that there are many solutions when it es to shuffling arrays.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745562488a4633190.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信