tgideas / motion

Motion is a javascript library based on Zepto for mobile
http://tgideas.github.io/motion
449 stars 112 forks source link

Animation组件对同个元素进行2次动画,第二次会无限循环 #2

Open nornor opened 9 years ago

nornor commented 9 years ago
window.s3LT = new mo.Animation({
        target: $('.s3_layer_lt'),
        fillMode: 'forwards',
        duration: 500,
        iteration: 1,
        keyframes: {
            0: {'opacity': '0', easing: 'ease-in-out'},
            100: {'opacity': '1'}
        }
    });
s3LT.on("end",function(){
window.s3LTBack = new mo.Animation({
    target: $('.s3_layer_lt'),
    fillMode: 'forwards',
    duration: 500,
    iteration: 1,
    keyframes: {
        0: {'opacity': '0', easing: 'ease-in-out'},
        100: {'transform': 'translate3d(0,0,0)', easing: 'ease-in-out'}
    }
});
})

第一个动画on end的时候再针对本身DOM初始化一个新的动画,此时head标签会无限插入新的animation

lyushine commented 9 years ago

可以考虑用class或者class里的属性来判别 end start避免无限循环 var $snowball = $("#snowball"); $snowball.on('webkitAnimationStart',function(e){ if($(this).hasClass("s2_snowball_out")){ score = score+randTempdom; sendScore(score); }else if($(this).hasClass("s2_snowball_in")){ randTempdom = random(); var snowUrl = ""; if(randTempdom == 50){ snowUrl = "http://7vihcx.com1.z0.glb.clouddn.com/gameA/mobile/images/s2_star5.png"; }else if(randTempdom <50&& randTempdom>19){ snowUrl = "http://7vihcx.com1.z0.glb.clouddn.com/gameA/mobile/images/s2_star4.png"; }else if(randTempdom <20&& randTempdom>7){ snowUrl = "http://7vihcx.com1.z0.glb.clouddn.com/gameA/mobile/images/s2_star3.png"; }else if(randTempdom <7&& randTempdom>2){ snowUrl = "http://7vihcx.com1.z0.glb.clouddn.com/gameA/mobile/images/s2_star2.png"; }else if(randTempdom){ snowUrl = "http://7vihcx.com1.z0.glb.clouddn.com/gameA/mobile/images/s2_star1.png"; } $(".s2_snowball img").attr("src",snowUrl); } });

$snowball.on('webkitAnimationEnd',function(e){
  if($(this).hasClass("s2_snowball_out")){
    $snowball.removeClass('s2_snowball_out');
    $snowball.addClass('s2_snowball_in');

  }else if($(this).hasClass("s2_snowball_in")){
    $snowball.removeClass('s2_snowball_in');
  }
});