我有下面的代碼獲取當前值:jQuery的動畫從相關陣列
var tot = 0;
var prices = {
"Price1:": 37152.32,
"Price2:": 54023,
"Price3:": 37261.75
};
var animationDuration = 2000; // Must be lower than carousel rotation
function animatePrices() {
var dur = animationDuration/Object.keys(prices).length;
for (var key in prices) {
tot += prices[key];
$({
p: 0
}).animate({
p: tot
}, {
duration: dur,
easing: 'linear',
step: function(now) {
document.getElementById("total").textContent = now.toFixed(2);
document.getElementById("what-for").textContent = key + " $" + prices[key];
}
});
}
}
window.onload = function() {
animatePrices();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
$<span id="total"></span>
<br>
<span id="what-for"></span>
但正如你所看到的,在無論什麼樣的跨度它顯示Price3。但是我希望它隨着價格的上漲而循環通過價格。 例如: 當現在是37100時,那麼什麼是Price1。 當現在是40000那麼什麼是Price2。 當現在是100000,那麼什麼是Price3。
我在做什麼錯?
其中是'now'變量? –
功能(現在)你的意思是? – MrDikke