JavaScript Greensock循环无法按预期工作

时间:2015-08-27 14:13:42

标签: javascript greensock

有没有人有任何想法为什么这不起作用?似乎达到了第一个背景,但随后没有改变位置。

// Avatar animations
var fadePulse           = true; // true: will fade avatar images and pulse in and out once changed
                                // false: will slide the avatars in and out 
var avatarCount         = 5;    // set the amount of avatars in the sprite image
var avatarSpeed         = 2     // set the avatar transition speed
var avatarHeight        = 250;  // set the height of a single avatar image
var avatars             = creative.avatars;
var animDuration        = 0.4;
var avatarCurrentIndex  = 0;

var avatarAni = new TimelineLite({ paused: true, repeat: -1 }); 

function startAvatarAnimation() {
    show(avatars);
    avatarAni.to(avatars, animDuration, {
        scaleX: 1, // 1.1 (for bouncing)
        scaleY: 1, // 1.1 (for bouncing)
        ease: Power3.easeIn,
        onComplete: onCompleteScaleIn
    });
    avatarAni.to(avatars, animDuration, {
        scaleX: 1,
        scaleY: 1,
        ease: Power3.easeOut
    });
    avatarAni.timeScale(avatarSpeed);
    avatarAni.play();
}

function onCompleteScaleIn() {
    avatarCurrentIndex ++;

    console.log(avatarCurrentIndex ++);
    if(avatarCurrentIndex <= avatarCount-1){ 
        TweenLite.set(avatars, {
            backgroundPosition: '0 -' + (avatarCurrentIndex * avatarHeight) + 'px'
        });
    } else { 
        avatarAni.pause();
        hide(avatars);
    }
}

这一切似乎与循环并调整位置的那部分区别开来。

1 个答案:

答案 0 :(得分:0)

// Avatar animations
var fadePulse           = true; // true: will fade avatar images and pulse in and out once changed
                                // false: will slide the avatars in and out 
var avatarCount         = 5;    // set the amount of avatars in the sprite image
var avatarSpeed         = 2     // set the avatar transition speed
var avatarHeight        = 250;  // set the height of a single avatar image
var avatars             = creative.avatars;
var animDuration        = 0.4;
var avatarCurrentIndex  = 0;
var i = 0;

var avatarAni = new TimelineMax({ paused: true, repeat: -1 });

function startAvatarAnimation() {
    show(creative.avatars);

    avatarAni.to(avatars, animDuration, { scaleX: 1, scaleY: 1, ease: Power3.easeIn, onComplete: onCompleteScaleIn });
    avatarAni.to(avatars, animDuration, { scaleX: 1, scaleY: 1, ease: Power3.easeOut });

    avatarAni.timeScale(avatarSpeed);
    avatarAni.play();
}

function onCompleteScaleIn() {
    i++;

    if(i <= avatarCount-1){ 
        TweenMax.set(avatars, { backgroundPosition: '0 -' + (i * avatarHeight) + 'px' });
    } else { 
        avatarAni.pause();
        hide(avatars);
    }
}
相关问题