2014-02-27 198 views
4

我有一個完全使用CSS動畫構建的無限滑塊,我希望能夠使用javascript(onlcick和鍵盤輸入)處理動畫。我希望能夠從一個幀快速運行動畫,也就是說,加速到另一個幀,以便在按下指向滑塊中的特定圖像的按鈕時提供跳躍效果。此外,我希望能夠通過鍵盤輸入步驟通過動畫...( - >,< - )使用JavaScript暫停CSS動畫並跳轉到動畫中的特定位置

有誰知道如何實現這一目標?

上點擊

..

  1. 檢索當前的關鍵位置;
  2. 設置適當的方向到目的地;
  3. 大幅增加動畫的速度,直到達到所需的關鍵幀;
  4. 可以暫停動畫或新點

鍵盤輸入繼續...

  1. 跳到特定的關鍵幀,並繼續動畫
+0

參見:http://stackoverflow.com/questions/20499462/css-keyframe-animations-and-延遲「不幸的是,目前沒有選擇可以輕鬆地在迭代之間延遲,但是您可以添加另一個具有相同值的停止點(如我評論),並增加持續時間:「 –

+0

[GSAP](http://www.greensock.com/gsap-js/)將是偉大的。看看TimelineMax –

回答

3

嘗試

HTML

<!-- html --> 
<div id=animated></div> 

CSS

 /* css */ 

    #animated { 

     animation : animationName; /* animation block(s) */ 

     animation-play-state : paused; /* (paused; running) */ 

    } 

    @keyframes animationName { 

     0% { .. /* css animations (`animationstart`) */ } 

     50% {..} 

     100% { .. /* css animations (`animationend`/`animationiteration` (start; end)) */ } 

    } 

JS

(function(el) { 

    function animations(e) { 

    /* `key code` to trigger event */ 
    if (e.which === 123456) { 

     $(el).animate({ /* jquery animations */ }, 123456 /* duration */) 
     .promise().done(function() { 
     el.style.animationPlayState = "running"; 
     el.style.WebkitAnimationPlayState = "running"; 
     }); 

     }; 

    }; 

$(window).on("keydown", animations); 
})($(#animated).get(0)) 

$(document).ready(function() { 

    (function reanimate(el, r, p, runner, pauser, pauseAll) { 

     var _state = function() { 
      $.when(
      $("#animated").data("states", {"fxq": "animated!","cssAnimationState": (el.style.WebkitAnimationPlayState || el.style.animationPlayState),"jsAnimationState": $("#animated").queue("fx")[0]})) 
      .done(function(status) { 
       return status.data("states") 
      }); 

      return String("css animation state: " + (el.style.WebkitAnimationPlayState || el.style.animationPlayState) + "&nbsp; js animation state: " + $("#animated").queue("fx")[0]) 
     }; 

     var runner = function() { 
      el.style.animationPlayState = "running"; 
      el.style.WebkitAnimationPlayState = "running"; 
      return $("data").html(_state()) 
     }; 

     var pauser = function() { 
      el.style.animationPlayState = "paused"; 
      el.style.WebkitAnimationPlayState = "paused"; 
      $("#animated:animated, #animated *").finish().queue("fx", []); 
      return $("data").html(_state()) 

     }; 

     $("button:last").on("click", pauser); 
     $("button:first").on("click", runner); 

     function player(e, pause, play, pauseAll) { 

      /*! 
      // settings 
      */ 
      var pauseAll = (undefined || 38); /* `up-arrow` : `pauseAll` */ 
      var pause = (undefined || 37); /* `left-arrow` : `paused` */ 
      var play = (undefined || 39); /* `right-arrow` : `running` */ 

      if (e.which === play) { 
       e.preventDefault(); 
       runner(); 
       $("data").html(_state()) 
      }; 
      /*! 
      // js (jquery) animations (, css transitions, 
      // css animations) at `paused` css animations 
      */ 
      if (e.which === pause) { 
       e.preventDefault(); 
       $.when(
       $('#animated') 
       .animate({ 
        width: "+=400px", 
        height: "+=400px", 
        borderRadius: "+=50%", 
        fontSize: "+=22px" 
       }, 
       { 
        duration: 3500, 
        easing: "swing", 
        start: $('#animated').css({"transition": "background 3500ms linear, box-shadow 3500ms linear","-webkit-transition": "background 3500ms linear, -webkit-box-shadow 3500ms linear","-moz-transition": "background 3500ms linear, -moz-box-shadow 3500ms linear","background": "yellow","box-shadow": "0.25em 0.25em 0.25em #f57900","-webkit-transform-style": "preserve-3d","-webkit-transform": "rotateX(180deg) rotateZ(45deg)","-moz-transform-style": "preserve-3d","-transform": "rotateX(180deg) rotateZ(45deg)","-webkit-backface-visibility": "visible","-moz-backface-visibility": "visible"}).html(function() { 
         return $("<em>" + $('#animated').data("states").fxq + "</em>").css({"display": "block","position": "relative","top": "25%","left": "0%","transform": "rototeX(33deg)","text-shadow": "2px 2px 2px orange"}).fadeIn(2000, function() { 
          _state() 
         }) 
        }) 
       }) 
       .animate({width: "100px",height: "100px", 
        borderTopLeftRadius: "0%", 
        borderTopRightRadius: "0%", 
        borderBottomLeftRadius: "0%", 
        borderBottomRightRadius: "0%", 
        fontSize: "10px"}, { 
        duration: 3500, 
        easing: "linear", 
        done: function() { 
         $('#animated').css({"transition": "background 3500ms ease-out, box-shadow 2500ms ease-out","-webkit-transition": "background 3500ms, -webkit-box-shadow 3500ms ease-out","-moz-transition": "background 3500ms ease-out, -moz-box-shadow 3500ms ease-out","background": "red","box-shadow": "0.0em 0.0em 0.0em transparent","-webkit-transform": "rotateX(0deg) rotateY(0deg) rotateZ(0deg)","transform": "rotateX(0deg) rotateY(0deg) rotateZ(0deg)","-moz-backface-visibility": "hidden","-webkit-backface-visibility": "hidden"}).children("em").fadeOut(2000, function() { 
          _state() 
         }).promise().done(function() { 
          $("em").finish().detach() 
         }) 
        } 
       }), $("data").html(_state())).promise().done(function() { 
        runner(); 
       }).always(function() {_state()}) 
      }; 
      /*! 
      // pause all css and js animations 
      */ 
      if (e.which === pauseAll) { 
       e.preventDefault(); 
       (function() { 
        var _check = ($("#animated").queue("fx")[0] != undefined ? $("#animated:animated, #animated *").finish() && pauser() : ((el.style.animationPlayState === undefined ? el.style.WebkitAnimationPlayState : el.style.animationPlayState) === "running" ? pauser() : runner())) 

        return $.when(_check, $("data").html(_state())) 

       }()) 
      }; 
     }; 
     $(window).on("keydown", player); 

     return $("data").html(_state()) 
    })($("#animated").get(0), "running", "paused") 
}) 

http://guest271314.github.io/reanimate/


有可能實現的要求, 包括CSSOM,使用Javascript,jQuery庫幾種可能的方式。請參閱鏈接。

可以通過 animation-play-state屬性完成啓動和暫停css動畫。

啓動和停止jquery動畫可以通過幾種方法完成。 $(element).queue("fx", [])應該停止所有jquery動畫並清除 jquery animationsqueue.finish()應該完成,或者finish目前運行 ,或者inprogress jquery動畫。

animation-play-state : paused不是停止jquery動畫。

該過程也可以通過插入,替換,替換 或刪除style元素或僅刪除style元素中的文本來完成; animationstartanimationiteration DOM事件;以及下面鏈接的其他一些潛在的選項 。

把reanimate.js放在一起,包括上面提到的方法的幾個 。 css轉換在jQuery動畫中包含 ,儘管該部分可能用 替代訪問運行css動畫(「@keyframes」)的 的確切時序階段,和/或插入新的動畫;或者調整或重新定義時間。

這件作品只是一個工作草案模板來分享起草功能。 請注意,webkit,firefox和opera可能會「略微」渲染動畫。 使用此模板webkit可能會出現「轉變」更多「更流暢」 到「運行」css動畫比firefox。完整的歌劇支持可能需要一些更精細的調整。

對於某些css屬性,Opera似乎使用-o--webkit-前綴,而對其他人僅使用w3c標準(無前綴)。此外,每個瀏覽器 可能需要不同的「前綴」,用於訪問「animationstart」等

reanimate.js當前不(版本1.0)嘗試內/的訪問的確切的CSS keyframe「(elapsedTimerunning css動畫中斷之前 他們與幾個layered或「reanimated」css/js動畫和/或css轉換。 但是,試圖將css動畫返回到它們在「分層」js動畫中斷之前運行的大致位置 。雖然, 可以訪問keyframes的確切動畫elapsedTimetimeStamp(可能是0或miiliseond類型的時間戳,具體取決於瀏覽器)。例如, 通過認定「動畫引用」或「動畫開始」或「動畫結束」事件, 或可能使用「requestAnimationFrame」。


資源:

programmatically changing webkit-transformation values in animation rules

Set Webkit Keyframes Values Using Javascript Variable 位置 http://jsfiddle.net/russelluresti/RHhBz/2/

Alter or generate and then remove a css3 keyframe

Changing the different Keyframes in css using Javascript

https://developer.apple.com/library/safari/documentation/AudioVideo/Reference/WebKitAnimationEventClassReference/WebKitAnimationEvent/WebKitAnimationEvent.html

http://msdn.microsoft.com/en-us/library/ie/hh772074%28v=vs.85%29.aspx

http://blogs.msdn.com/b/msdn_answers/archive/2013/11/04/part-i-using-javascript-to-set-keyframes-in-css-animations-windows-store-apps-ie.aspx

http://www.w3.org/TR/animation-timing/

希望這有助於