gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:51:46 25 4
gpt4 key购买 nike

函数调用存在问题,在页面加载时在初始函数调用中第一次调用变换图像旋转步骤后,我的变换图像旋转步骤不起作用。每次我构建的图表上的任何值发生更改时都会调用此函数。为了绘制一幅画,有两枚硬币从屏幕顶部掉落,然后旋转两次,然后落在一堆硬币上。相反,每次调用该函数时,硬币只会从屏幕顶部掉落/动画。

<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 上的各种方法。似乎没有任何效果,所以我希望另一双眼睛能发现我的问题。提前致谢!

最佳答案

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

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>

关于javascript - jquery 动画与图像旋转在第一次函数调用后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42628389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com