2011-12-22 18 views
2

最近我问了这个问题: Would like to understand the Animate function (calculation and stepping) 我得到了答案。有人可以给我一个独立代码的jQuery动画功能

我试图删除不必要的jQuery代码,只留下jQuery动画功能。

如果有人可以提供我只有具有他技术的jQuery动画功能 - 我会非常感激。

+0

你想要什么? – Richard 2011-12-22 19:56:11

+0

'.animate'方法依赖于jQuery的其他部分;你为什么要“独立”? – Shad 2011-12-22 19:57:01

+0

“不必要的代码”是什么意思?正如预料的那样,jQuery的动画功能依赖于库的其他部分。阻止你整体包含jQuery库吗? – 2011-12-22 19:57:14

回答

3

创建动画其实是非常简单的。设置的时间间隔递归地改变元素的CSS属性,让你的函数运行:

var distance  = 300, 
    frames  = 30, 
    current_frame = 0, 
    time   = 1000, 
    delta   = Math.floor(distance/frames), 
    $element  = $('#my-element'), 
    my_timer; 

my_timer = setInterval(function() { 

    //make sure the end of the animation has not been reached 
    if (current_frame < frames) { 

     //get the current property you want to animate and add to it the `delta` variable which is how much the property should change in each iteration 
     var left = parseInt($element.css('left').replace('px', ''), 10); 
     $element.css('left', (left + delta)); 
    } else { 

     //the end of the animation has been reached so stop the interval 
     clearInterval(my_timer); 
    } 
    current_frame++; 
}, Math.floor(time/frames)); 

这里是一个演示:http://jsfiddle.net/aDLbK/1/

中 - 当然,这仍然使用jQuery的.css()功能,但你可以删除单条线放置在你想操纵元素的任何JavaScript中。

相关问题