2011-12-22 47 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中。

相關問題