2014-03-04 63 views
0

我試着去創建一個動畫過渡,然後一旦做到了這一點,動畫旋轉回它的出發點和無限循環,這樣的功能。循環功能,CSS動畫旋轉

我有以下的只有我不能讓動畫工作,也沒有恢復爲默認?

http://jsfiddle.net/QfeC2/

function drunk(){ 
    $('div').css({'-webkit-transform':'rotate(1deg)'}); 
    $('div').delay(4000).css({'-webkit-transform':'rotate(0deg)'}); 
} 

setTimeout(function() { drunk(); }, 2000); 

回答

0

如果你使用的CSS動畫,爲什麼不使用純CSS? 你可以將動畫屬性包裝在一個類中,並在JS中切換該類。

div { 
    -webkit-animation:anim 2s ease infinite; 
} 
@-webkit-keyframes anim 
{ 
    0 {-webkit-transform:rotate(0deg);} 
    50% {-webkit-transform:rotate(1deg);} 
    100% {-webkit-transform:rotate(0deg);} 
} 

Updated fiddle

1

.delay()只適用於當您使用jQuery的動畫,你必須使用的setTimeout

function drunk() { 
    $('div').css({ 
     '-webkit-transform': 'rotate(1deg)' 
    }); 
    setTimeout(function() { 
     $('div').css({ 
      '-webkit-transform': 'rotate(0deg)' 
     }); 
    }, 4000); 
} 

setTimeout(function() { drunk(); }, 2000); 

DEMO

使用的setInterval爲連續循環

setInterval(function(){drunk();},8000); 

DEMO

+0

感謝@Anton,我想不過 – Liam

+0

@Liam你希望它看起來像這一點,平穩地旋轉? http://jsfiddle.net/QfeC2/8/ – Anton

+0

@Liam或者你可以使用純CSS http://jsfiddle.net/QfeC2/10/ – Anton

0

看到更新後的提琴: http://jsfiddle.net/QfeC2/3/

function AnimateRotate(angle) { 
// caching the object for performance reasons 
var $elem = $('div'); 

// we use a pseudo object for the animation 
// (starts from `0` to `angle`), you can name it as you want 
$({deg: 0}).animate({deg: angle}, { 
    duration: 2000, 
    step: function(now) { 
     // in the step-callback (that is fired each step of the animation), 
     // you can use the `now` paramter which contains the current 
     // animation-position (`0` up to `angle`) 
     $elem.css({ 
      transform: 'rotate(' + now + 'deg)' 
     }); 
    }, 
    complete: function(){ 
     AnimateRotate(360); 
    } 
}); 
} 
AnimateRotate(360); 

UPDATE

旋轉每個週期後回:

http://jsfiddle.net/QfeC2/9/

var boolDirection = true; 

function AnimateRotate(angle) { 
// caching the object for performance reasons 
var $elem = $('div'); 

// we use a pseudo object for the animation 
// (starts from `0` to `angle`), you can name it as you want 
$({deg: 0}).animate({deg: angle}, { 
    duration: 2000, 
    step: function(now) { 
     // in the step-callback (that is fired each step of the animation), 
     // you can use the `now` paramter which contains the current 
     // animation-position (`0` up to `angle`) 
     $elem.css({ 
      transform: 'rotate(' + now + 'deg)' 
     }); 
    }, 
    complete: function(){ 
     if(boolDirection) 
     { 
     AnimateRotate(-360); 
      boolDirection = false; 
     } 
     else 
     { 
      AnimateRotate(360); 
      boolDirection=true; 
     } 
    } 
}); 
} 
AnimateRotate(360); 
+0

這是線沿線的什麼我後@JFit只是我不能讓它旋轉,然後旋轉回來,它旋轉,然後回到其默認位置1跳躍 – Liam

+0

ahh ok .. gimmie幾秒更新。 –

+0

現在它會來回 - 看到更新的小提琴@Liam –