2017-07-16 70 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