2017-07-16 69 views
0

我寫了一個jQuery緩動功能的小動畫庫。 現在我有問題,當我的起始值大於或等於最終值時,我的函數dosnt工作。nodejs反向計數(增加/減少)

var easing = require("../easing.js").ease; 
 

 

 
function fade(start_value, end_value, duration) { 
 

 
    var interval = 46; 
 
    var time = 0; 
 

 

 
    setInterval(function() { 
 

 
    // calc current time 
 
    time = time + interval; 
 

 
    // call easing lib 
 
    value = easing["linear"](time, start_value, end_value, duration); 
 
    value = Math.round(value); 
 
    
 
    if(value >= end_value){ 
 
     clearInterval(this); 
 
     console.log("Done"); 
 
    } 
 
    
 
    console.log(value); 
 

 
    }, interval); 
 
} 
 

 

 
fade(255, 0, 1000); // 255 immediately 
 
fade(0, 255, 1000); // 0 to 255 in 1s

我希望在起始值爲greather那麼最終值遞減計數。 我如何使用easing lib做到這一點?

非常感謝

+0

你能提供一個有效的代碼片段嗎?我收到一條錯誤消息。 – ideaboxer

回答

0

你的問題是此行

if(value >= end_value){ 

在這裏,你與你正在向上走向end_value假設寫的。相反,該線應該不可知的是start_value大於還是小於end_value。你的功能應該是這樣的:

function fade(start_value, end_value, duration) { 

    ... 

    var min_value = Math.min(start_value, end_value); 
    var max_value = Math.max(start_value, end_value); 

    ... 

    // Stop the animation when out of range 
    if (value <= min_value || value >= max_value) { 
    clearInterval(this); 
    console.log("Done"); 
    } 

    ... 

} 
+0

對不起,但這dosnt工作。這裏來自「easing」方法的「線性」:函數linear(t,b,c,d){return c * t/d + b; }',我認爲問題是這個函數,如果你像「線性(0,255,0,1000)」那樣調用這個函數,它立即返回「255」 – Marc