var container = $(".coins");
var lastTime = new Date().getTime();
var count = 0;
var hand = $(".hand");
function loop(){
// 控制时间间隔 保证1秒最多60帧
var now = new Date().getTime();
if(now - lastTime < 17){
requestAnimationFrame(loop);
return;
}
lastTime = now;
// 执行正常每一帧的逻辑
if(count-- == 0){
var coin = $("
")
coin.css({left:Math.random()*500, top:0});
container.append(coin);
count = 30;
}
container.children().each(function(){
$(this).css({ top: parseInt($(this).css("top"))+5 });
if(parseInt($(this).css("top")) > 500){
var offsetX = parseInt($(this).css("left")) - parseInt(hand.css("left"));
debugger;
if(offsetX>50 || offsetX<-50){
$(this).css({background:"#999999"});
}else{
$(this).remove();
}
}
});
// 播放下一帧动画
requestAnimationFrame(loop);
}
loop();
$(document).on("mousemove", function(e){
hand.css({left : e.clientX});
});