jquery动画与图像旋转在第一次函数调用后不起作用

时间:2017-03-06 14:40:53

标签: javascript jquery jquery-animate css-transforms

函数调用存在一个问题,我的转换图像旋转步骤在我的页面加载时第一次调用初始函数调用后不起作用。每次在我构建的图形上更改任何值时,都会调用此函数。要绘制图片,有两个硬币从屏幕顶部掉落,然后在它们落在一堆硬币上之前旋转两次。相反,这样工作,每次调用函数时,硬币只会从屏幕顶部掉落/动画。

<div id="secondCoin"><img class="coin" src="./images/one_coin.png" alt="Second Coin Drops" /></div>
<div id="firstCoin"><img class="coin" src="./images/one_coin.png" alt="First Coin Drops" /></div>
<div id="showCoins"><img class="coin-stack" src="./images/fullstack_coins.png" alt="Stack of Coins" /></div>

function coinStack(height){
var coinSound = document.getElementById('coin-sound');
var rotateDeg = 720;
// prevents sound play on multiple clicks
coinSound.pause();
coinSound.currentTime = 0;
// causes coin stack to slide upward
$("#showCoins").css("display", "inline-block");
$("#showCoins").css("top", height + "px");
screenWidth <= 400 ? $("#showCoins").stop().animate({top: '3'},1000) : $("#showCoins").stop().animate({top: '0'},1000);
// first coin drops
$("#firstCoin").css("display", "inline-block");
$("#firstCoin").css("top", "-324px");
$("#firstCoin").clearQueue().delay(1000).animate({top: '10', deg: rotateDeg},{
    duration: 350, 
    step: function(now){
        $("#firstCoin").css({ transform: "rotate(" + now + "deg)" });
    }
});
// second coin drops
$("#secondCoin").css("display", "inline-block");
$("#secondCoin").css("top", "-324px");
$("#secondCoin").clearQueue().delay(1400).animate({top: '0', deg: rotateDeg},{
    duration: 350, 
    step: function(now){
        $("#secondCoin").css({ transform: "rotate(" + now + "deg)" });
    }
});
// coin sound effect
coinSound.volume = 1.0;
coinSound.play(); 

}

我尝试过使用.stop(),. clearQueue(),以不同的方式设置/删除转换属性,并检查了stackoverflow上的各种方法。似乎没有任何工作,所以我希望另一双眼睛会发现我的问题。提前谢谢!

2 个答案:

答案 0 :(得分:1)

这可能有点迟了,但您需要做的是为每次迭代为动画添加另外两个旋转。这将解决您的问题,您可以一遍又一遍地调用相同的功能。

&#13;
&#13;
var rotateDeg = 720; //Declare this outside the function

$('button').click(function() {
  coinStack(400);
})

function coinStack(height) {

  $("#showCoins").css("display", "inline-block");
  $("#showCoins").css("top", height + "px");
  $("#firstCoin").css("display", "inline-block");
  $("#firstCoin").css("top", "-324px");
  
  $("#firstCoin").clearQueue().delay(1000).animate({
    top: '10',
    deg: rotateDeg
  }, {
    duration: 3500,
    step: function(now) {
      $("div").css({
        transform: "rotate(" + now + "deg)"
      });
    }
  });
  
  rotateDeg += 720; //For every animation you add another 720 degrees
};

$("#secondCoin").css("display", "inline-block");
$("#secondCoin").css("top", "-324px");
$("#secondCoin").clearQueue().delay(1400).animate({top: '0', deg: rotateDeg},{
    duration: 350, 
    step: function(now){
        $("#secondCoin").css({ transform: "rotate(" + now + "deg)" });
    }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="secondCoin"><img class="coin" src="#" alt="Second Coin Drops" /></div>
<div id="firstCoin"><img class="coin" src="#" alt="First Coin Drops" /></div>
<div id="showCoins"><img class="coin-stack" src="#" alt="Stack of Coins" /></div>

<button>Push to rotate</button>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

无论页面状态如何,您似乎都在执行代码。

尝试在此处插入代码:

$(document).ready(function(){
//your code here
})

有关更多信息,请参阅: https://learn.jquery.com/using-jquery-core/document-ready/