2017-04-20 40 views
2

我有一個只能運行一次的動畫。我想點擊按鈕重複動畫。我不知道我做錯了什麼。非常感謝你。使用ng-class單擊按鈕再次生成動畫。 Angular.js

<div ng-controller="MyCtrl"> 
<div class='contendor_portafolio' class='next'> 
<video id='video' width="320" height="240" ng-class='transicion' controls> 
    <source src="video.mp4" type="video/mp4" /> 
</video> 
</div> 
<button ng-click="fn_transicion()">again</button> 

var myApp = angular.module('myApp',[]); 
function MyCtrl($scope) { 
$scope.fn_transicion= function(sentido){ 
     $scope.transicion="next"; 
} 
} 
    #video{ 
    width: 98%; 
    height: 100%; 
    transition: all linear 0.5s; 
    background:blue; 
    -webkit-animation: next 1s 1; /* Safari 4+ */ 
    } 

    @-webkit-keyframes next { 
    25% { 
     -webkit-transform: translateX(1700px); 
    } 
    50% { 
     opacity: 0; 
     -webkit-transform: translateY(-2000px); 
     display: none; 
    } 
    60% { 
     -webkit-transform: translateX(-1700px); 
    } 
    100%{ 
    } 
    } 

我需要使用納克級。

http://jsfiddle.net/wmvfx5cd/

回答

1

添加CSS來一個類實例.run-animation,添加和刪除按鈕

$scope.fn_transicion= function(sentido){ 
    $scope.transicion="next"; 
     var el = document.getElementById('video'); 
    // -> removing the class 
    el.classList.remove("run-animation"); 
    void el.offsetWidth; 
    // -> and re-adding the class 
    el.classList.add("run-animation"); 
} 

Check the working Fiddle

更新答案點擊: 使用NG-

HTML

<div ng-controller="MyCtrl"> 
    <div class='contendor_portafolio' class='next'> 
    <video id='video' width="320" height="240" ng-class="{'run-animation': transicion === true}" controls> 
     <source src="video.mp4" type="video/mp4" /> 
    </video> 
    </div> 
    <button ng-click="fn_transicion()">again</button> 
</div> 

JS

var myApp = angular.module('myApp',[]); 
myApp.controller("MyCtrl", ['$scope','$timeout', 
function($scope,$timeout) { 
$scope.transicion=true; 
    $scope.fn_transicion= function(sentido){ 
     $scope.transicion=false; 
     $timeout(function() { 
     $scope.transicion=true; 
     }, 100); 
    } 

}]); 

Updated Fiddle

+0

或多或少是我想要的,但我需要的,如果或者使用時,NG-類。這是我在我的真實項目中遇到的一個小問題。 – yavg

+0

解決方案不適合你嗎? –

+0

不是我所希望的。我需要使用ng-class來做到這一點。 – yavg