逐帧动画的使用

1、如果对性能要求不高可以直接使用jquery的animate

  $(".nowGift" + heartNumber + "").animate({ right: (x += x_step) + 'px', bottom: (y += a * x_step + 1.5) + 'px', opacity: (300 - y) / 300 }, speed, function () {
    if (y > 300) {
      $(".nowGift" + heartNumber + "").remove();
    }else{
      count++;
      heartMove(x, y, a, x_step, heartNumber, speed, count);//回调函数。
    }
  });

 注意:这会导致jQuery的bug,窗口失去焦点时停止触发。

2、可以使用第二种方案,直接替代jquery的animate使用velocity.js直接替代animate

  $(".nowGift" + heartNumber + "").velocity({ right: (x += x_step) + 'px', bottom: (y += a * x_step + 1.5) + 'px', opacity: (300 - y) / 300 }, speed, function () {
    if (y > 300) {
      $(".nowGift" + heartNumber + "").remove();
    }else{
      count++;
      heartMove(x, y, a, x_step, heartNumber, speed, count);//回调函数。
    }
  });

 性能会更好,而且不会引发那个bug

3、可以直接使用css3的transition或者使用setTimeout

  $(".nowGift" + heartNumber + "").css({'right': (x += x_step) + 'px', 'bottom': (y += a * x_step + 1.5) + 'px', 'opacity': (300 - y) / 300 })
  if (y > 300) {
    $(".nowGift" + heartNumber + "").remove();
  }else{
    setTimeout(function(){
      count++;
      heartMove(x, y, a, x_step, heartNumber, speed, count);//回调函数。
    },20)
  }

  效果可能会差一点需要调节,不过性能方面也是可以的  

posted @ 2018-08-22 15:21  kimingw  阅读(286)  评论(0)    收藏  举报