我有一個不錯的主意,即使用可以在SVG對象上使用的'stroke-dasharray'CSS屬性來描述頁面上的路徑,爲形狀的出現提供一個很好的藝術方式。什麼是最簡潔的方式來動畫沒有.animate()jQuery?
它的工作原理非常完美,並且受到大多數現代瀏覽器和手機的支持。
的我做了什麼一個很好的的jsfiddle可以在這裏找到:http://jsfiddle.net/G6ECE/
,使整個事情現在勾選代碼:
num=0;
setInterval(function(){
updateStroke(num);
num+= 0.2;
}, 1);
function updateStroke(num){
// stroke-dasharray is a list of two or more numbers. In this example, percentage values are used to make all paths fully stroked at the end of the animation, irregardless of path length.
$('svg path').css({stroke:'#00FF00','stroke-dasharray':num+'% '+(100-num)+'%'});
}
顯然,這是一個非常基本的,醜陋的方式動畫的東西。
我想自定義一點動畫:我希望能夠添加自定義動畫長度並使用不同的緩動。不幸的是,我通常用來爲CSS屬性設置動畫效果的jQuery $().animate()函數不支持非數字值。
由於stroke-dasharray使用兩個或多個數字(像素或百分比)值來工作,我需要一個替代的.animate()
什麼是做到這一點的乾淨的方式? (使用盡可能多的現有jQuery功能,並儘可能少地重新創建動畫框架)
而不是'setInterval'你應該使用'requestAnimationFrame',參見http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/。 –