简体   繁体   English

角度ng级的CSS过渡淡入

[英]css transition fade in angular ng-class

I'm trying to mock a refresh for a list that I have. 我正在尝试模拟刷新列表。 So when a user clicks refresh, the list fades in to let users know a new GET request was made. 因此,当用户单击“刷新”时,列表会淡入以让用户知道发出了新的GET请求。

html HTML

<div ng-controller="ModalDemoCtrl">

    <ul ng-repeat="friend in friends" class="animated" ng-class="{fadeIn: refresh === true}">
       <li>{{friend}}</li>
    </ul>

<button ng-click="refresh=!refresh"> Fade me in every time </button>

</div>

css CSS

.animated { 
    -webkit-animation-duration: 1s; 
    animation-duration: 1s; 
    -webkit-animation-fill-mode: both; 
    animation-fill-mode: both; 
} 

@-webkit-keyframes fadeIn { 
    0% {opacity: 0;} 
    100% {opacity: 1;} 
} 
@keyframes fadeIn { 
    0% {opacity: 0;} 
    100% {opacity: 1;} 
} 
.fadeIn { 
    -webkit-animation-name: fadeIn; 
    animation-name: fadeIn; 
}

and of course the js 当然还有js

angular.module('plunker', ['ui.bootstrap']);
var ModalDemoCtrl = function ($scope, $modal, $log) {

  $scope.friends = ['andy', 'randy', 'bambi'] ;

};

Here's a working plnkr: http://plnkr.co/edit/R0Gv2T?p=info 这是一个有效的plnkr: http ://plnkr.co/edit/R0Gv2T?p=info

My issue is the way I have it, the user has to click twice to fade in the list in after clicking it once because the variable refresh has to be set back to false before being set back to true. 我的问题是我拥有它的方式,用户必须单击两次才能在列表中单击一次以淡入列表,因为在将变量refresh设置回true之前,必须将其设置为false。 Is there anywhere through pure css to accomplish this? 是否有任何地方可以通过纯CSS做到这一点? I've tried setting scope variables in the controller and calling on click function but I'm just confused at this point. 我试过在控制器中设置作用域变量并调用click函数,但此时我很困惑。 I'm open to any solution 我愿意接受任何解决方案

For one, I'd check out angular-motion as it has all of these kinds of css animations. 首先,我将检查角度运动,因为它具有所有这些css动画。 If not though you should hook into the ngAnimate module as it provides a great way to handle this sort of stuff using ng-hide/ng-show. 如果不是这样,则应该加入ngAnimate模块,因为它提供了一种使用ng-hide / ng-show处理此类内容的好方法。 So your css becomes 所以你的CSS变成

.fadeIn { 
    opacity: 0;
}
.fadeIn.ng-hide-remove {
    -webkit-animation-name: fadeIn; 
    animation-name: fadeIn;
}

and your html becomes 和你的HTML成为

<ul ng-repeat="friend in friends" class="animated" ng-show="refresh">
   <li>{{friend}}</li>
</ul>

You may also want to provide a fadeOut animation as well and apply it using css rule .fadeIn.ng-hide 您可能还需要提供一个fadeOut动画,并使用css规则.fadeIn.ng-hide应用它

I used @PLS timeout idea but kept the ng-class and added a variable that checks if it's on or off. 我使用了@PLS超时概念,但保留了ng-class并添加了一个变量来检查它是打开还是关闭。 Then always set it to false when you click before waiting for the timeout. 然后,在等待超时之前单击时,请始终将其设置为false。

  $scope.friends = [{name:'andy'}, {name:'randy'}, {name:'bambi'}] ;
    $scope.refresh = function(){
        $scope.fadeCheck = true;
        $timeout(function(){
            $scope.fadeCheck = false;
        }, 300);
    }

and the html 和HTML

<ul ng-repeat="friend in friends" class="animated" ng-class="{fadeIn: fadeCheck === false}">
  <li>{{friend.name}}</li>
</ul>

http://plnkr.co/edit/Xp7E6s?p=preview http://plnkr.co/edit/Xp7E6s?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM