私は、かなり単純なjavascript関数をいくつか使用して、ユーザーのアクションに基づいて上下に移動する数値のアニメーションを作成します。ページには、コールバック内でrecalculateDiscount()を呼び出すスライダがいくつかあります。これらのスライダは、その選択に基づいて数値をアニメーション化します。Javascriptを使用したアニメーターカウンター
var animationTimeout;
// Recalculate discount
function recalculateDiscount() {
// Get the previous total from global variable
var previousDiscount = totalDiscount;
// Calculate new total
totalDiscount = calculateDiscount().toFixed(0);
// Calculate difference
var difference = previousDiscount - totalDiscount;
// If difference is negative, count up to new total
if (difference < 0) {
updateDiscount(true, totalDiscount);
}
// If difference is positive, count down to new total
else if (difference > 0) {
updateDiscount(false, totalDiscount);
}
}
function updateDiscount(countUp, newValue) {
// Clear previous timeouts
clearTimeout(animationTimeout);
// Get value of current count
var currentValue = parseInt($(".totalSavingsHeader").html().replace("$", ""));
// If we've reached desired value, end
if (currentValue === newValue) { return; }
// If counting up, increase value by one and recursively call with slight delay
if (countUp) {
$(".totalSavingsHeader").html("$" + (currentValue + 1));
animationTimeout = setTimeout("updateDiscount(" + countUp + "," + totalDiscount + ")", 1);
}
// Otherwise assume we're counting down, decrease value by one and recursively call with slight delay
else {
$(".totalSavingsHeader").html("$" + (currentValue - 1));
animationTimeout = setTimeout("updateDiscount(" + countUp + "," + totalDiscount + ")", 1);
}
}
大部分のスクリプトはうまく動作しますが、いくつか問題があります。まず、古いブラウザではアニメーションの速度が遅くなり(IE6 & 7)、ユーザーがまだアニメーション内にあるときにスライダを再び動かすと混乱します。
新しいブラウザは、ユーザーがスライダの中央アニメーションを動かすと、間違った方向に進行しているように見えることがあります。したがって、updateDiscount()は新しい値で呼び出され、ディレクティブはダウンの代わりにカウントアップします。その結果、アニメーションは間違った方向にカウントされても正しい値に到達しないため、無限ループで間違った方向に移動します。
なぜこのようなことが起こるのか困惑しています。私のsetTimeout()の経験は非常に低く、問題である可能性があります。十分な情報を提供していない場合は、私に知らせてください。
ありがとうございました:)
私は私たちがコードの残りの部分を参照する必要があると思うのparticualriyアニメーションロジック。他のアニメーションがトリガされたときなどにアニメーション化されていることを検出すると、おそらく 'jQuery.stop()'を呼び出す必要があります。また、あなたは何かをタイムアウトさせるような設定をしています...それは、ロジックの中で非常に醜いものです: 'setTimeout(function(){updateDiscount(countUp、totalDiscount);}、1);' – prodigitalson