2015-09-09 48 views
4

,直到昨天我有一些幫助,以實現這一目標: http://jsfiddle.net/hopkins_matt/513ng07d/(感謝馬特·霍普金斯)-----創建紡紗號總數達到

function numberWithCommas(x) { 
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
} 

function timeSince() { 
    var prevTime = new Date(2015,8,8,0,0); 
    var thisTime = new Date(); 
    return (thisTime.getTime() - prevTime.getTime())/1000; 
} 

function parcelCount() { 
    var secDiff = timeSince(); 

    var leftNum = document.getElementById("left"); 
    var midNum = document.getElementById("mid"); 
    var leftNumCount = Math.round(((76/60) * secDiff) + 40093794); 
    var midNumCount = Math.round(((43/60) * secDiff) + 22874098); 

    leftNum.innerHTML = numberWithCommas(leftNumCount); 
    midNum.innerHTML = numberWithCommas(midNumCount); 
} 
parcelCount(); 
setInterval(parcelCount, 1000); 

我也想創建一個旋轉直到總決賽數字已達...

IE 我們已發貨到190個國家,當屏幕上顯示文字時,是否可以在0-190之間旋轉此數字?

所以它會旋轉數字,直到達到190,然後停止。

任何幫助,將不勝感激:)

回答

2

如果你只想從0 - 190的動畫,你可以使用這個。

只需循環一個函數即可增加顯示變量。

var totalShipped = 190; 
 
var shippedDisplay = 0; 
 
var shippedStep = totalShipped/(2 * 1000/100); // Animation duration 2 sec 
 
function animateShipped() { 
 
    if (shippedDisplay > totalShipped) 
 
    shippedDisplay = totalShipped; 
 
    document.getElementById("shipped").innerHTML = Math.round(shippedDisplay); 
 
    if (shippedDisplay < totalShipped) { 
 
    shippedDisplay += shippedStep; 
 
    setTimeout(animateShipped, 100); 
 
    } 
 
} 
 
animateShipped();
<h3>Shipped</h3> 
 
<span id="shipped"></span>

+0

感謝您的 - 有沒有辦法當頁面上達到了元件只觸發此(像lazyload效果)? –

+0

簡單的方法可以在這裏找到http://stackoverflow.com/questions/5911138/jquery-trigger-function-when-element-is-in-viewport,只需調用'animateShipped()'。你可能想要設置一個全局變量來記住它是否已經動畫,比如'if(!window.shippedAnim){window.shippedAnim = true; animateShipped()}' – GramThanos