简体   繁体   English

第一个函数调用后,带有图像旋转的jQuery动画不起作用

[英]jquery animate with image rotate doesn't work after first function call

There is an issue with a function call I have that my step for the transform image rotate isn't working after the first time it's called on an initial function call when my page loads. 函数调用存在问题,我的页面加载后在第一次调用初始函数调用后,变换图像旋转的步骤不起作用。 This function is called every time any value is changed on the graph I built. 每当我在构建的图形上更改任何值时,都会调用此函数。 To paint a picture there are two coins that drop from the top of the screen and then rotate twice before they land on a stack of coins. 要绘制图片,有两个硬币从屏幕顶部掉落,然后旋转两次,然后降落到一堆硬币上。 Instead working like that, the coins only drop/animate from the top of the screen every time the function is called. 代币那样工作,每次调用该函数时,代币仅从屏幕顶部掉落/动画。

<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(); 

} }

I've tried using .stop(), .clearQueue(), setting/removing the transform property differently, and checked various methods on stackoverflow. 我试过使用.stop()、. clearQueue(),以不同方式设置/删除transform属性,并检查了stackoverflow上的各种方法。 Nothing seems to be working so I'm hoping another set of eyes will spot my issue. 似乎没有任何效果,所以我希望另一双眼睛能够发现我的问题。 Thanks in advance! 提前致谢!

This can be a little bit late, but what you need to do is to add another two rotations to your animation for each iteration. 这可能会有点晚,但是您需要做的是为每次迭代在动画中添加另外两个旋转。 That will solve your problem and you can call the same function over and over again. 这样可以解决您的问题,您可以反复调用相同的函数。

 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)" }); } }); 
 <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> 

It looks like you're executing your code regardless the state of your page. 无论页面的状态如何,您似乎都在执行代码。

Try inserting your code inside this: 尝试在其中插入代码:

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

For more information see: https://learn.jquery.com/using-jquery-core/document-ready/ 有关更多信息,请参见: https : //learn.jquery.com/using-jquery-core/document-ready/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM