【问题标题】:AngularJS - Change Sprite Animation after it has finishedAngularJS - 完成后更改 Sprite 动画
【发布时间】:2015-05-30 16:54:02
【问题描述】:

我正在为我的登录页面制作精灵动画。我有两个精灵动画。第一个动画将开始并播放一次,另一个动画将在第一个动画结束后无限循环。

我知道你会切换类来切换精灵动画,但是我怎样才能让 Angular 监听第一个精灵动画的最后一个关键帧呢?

var app = angular.module('app', ['ui.bootstrap']);

app.controller('animation-toggle', function($scope) {
  $scope.class_status = 0;

  
  //toggle class with boolean
  $scope.toggleSingleClass = function() {
    $scope.class_status = !$scope.class_status;
    };
  
  //remove class
  $scope.removeSingleClass = function() {
    $scope.class_status =0;
    };
  //add class
  $scope.addSingleClass = function() {
    $scope.class_status = 1;
    };
});
/* FIRST ANIMATION */
.land-animation {
    width: 700px;
    height: 493px;
    margin: 60px auto;
    background: url('../images/font-animation.jpg') left center;
    animation: play 4.0s steps(48);
}

@keyframes play {
    100% { background-position: -33600px; }
}

/* SECOND ANIMATION */
.land-animation2 {
    width: 700px;
    height: 493px;
    margin: 60px auto;
    background: url('../images/font-animation2.jpg') left center;
    animation: play 4.0s steps(48) infinite;
}

@keyframes play {
    100% { background-position: -33600px; }
}
<!-- RUNNING ANGULAR 1.3.15 -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>


<!-- DIV ANIMATION -->
<div ng-controller="animation-toggle" ng-class="{'land-animation2': class_status }" class="hero-unit land-animation"></div>

【问题讨论】:

    标签: css angularjs animation sprite


    【解决方案1】:

    你可以试试纯jQuery的方式,只需要在控制器外包含jquery.min.js和下面的代码,

    $(document).ready(function(){
        $(".hero-unit.land-animation").one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){ 
           $(this).removeClass("land-animation").addClass("land-animation2");
       });
    });
    

    更新

    为避免中间出现白框,请将代码更改如下,

    $(document).ready(function(){
        $(".hero-unit.land-animation").one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){ 
           $(this).addClass("land-animation2");
       });
    });
    

    在 CSS 中,

    /* FIRST ANIMATION */
    .land-animation {
        width: 700px;
        height: 493px;
        margin: 60px auto;
        background: url('http://www.jpl.nasa.gov/spaceimages/images/mediumsize/PIA17011_ip.jpg') left center;
        animation: play 4.0s steps(48);
    }
    
    @keyframes play {
        100% { background-position: -33600px; }
    }
    
    /* SECOND ANIMATION */
    .land-animation2 {
        background: url('http://cdn.phys.org/newman/gfx/news/hires/2013/spitzerandal.jpg') left center;
        animation: play2 4.0s steps(48) infinite;
    }
    
    @keyframes play2 {
        100% { background-position: -33600px; }
    }
    

    【讨论】:

    • 哦,好吧,我被告知添加 jQuery 不会与 angular 很好地混合。是这样吗?如果没有,那么我将继续将 jQuery 添加到我的项目中。
    • Nope...jquery 不会影响项目,只要你在上面的情况下使用它。
    • 好的,有没有一种方法可以帮助您只使用 Angular?
    • 可以使用angularJS的ngAnimate回调功能
    • 我刚刚接受了你的 Jquery 答案,效果很好。但是,我有最后一个问题。在最后一帧,它变成白色,持续一帧,然后播放下一个动画。我怎样才能摆脱那个白框?
    猜你喜欢
    • 1970-01-01
    • 2014-01-22
    • 2016-01-23
    • 2014-01-16
    • 2017-03-18
    • 2012-01-06
    • 2019-10-11
    • 2019-05-14
    • 1970-01-01
    相关资源
    最近更新 更多