Animating CSS in AngularJS and Angular 2

6 篇文章 0 订阅
5 篇文章 0 订阅

Resume

In 2014, Google has annonced its’ new front-end framework: Angular 2 which is not completely compatible with AngularJS. This passage will try to compare animation in each framework.


AngularJS

The code below is an implementation with AngularJS. Since it’s not very complicated, we will write everything in only one html file.

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.js"></script>
    <style>    
        .drawer {
            transition: all ease 0.25s;
            background-color: skyblue;
            height: 90%;
            width: 200px;
            position: absolute;
            right: 0px;
        }

        .ng-hide {
            right: -200px;
        }
    </style>
</head>
<body ng-app="ngAnimate" ng-init="hidden=true" style="overflow: hidden;">
    <button ng-click="hidden=!hidden">Hide</button>
    <div class="drawer" ng-hide="hidden"></div>
    <p>Hidden: {{hidden}}</p>
</body>

As you can see, we should use ng-app="ngAnimate" to declare that this is an AngularJS Animation app. Then, use ng-init directive to initialize the variable which named hidden.
Afterward, we use ng-click to reverse to value of hidden (which is a boolean).
Finally, we use ng-hide to verify whether hidden is true. If hidden is true, angular will automatically invoke the class ng-hide in CSS. So the div will be push to the right corner.


Angular 2

Since we have already have style files, angular files above. I suppose you have already known how to create an Angular 2 project, so we wouldn’t write again this kind of thing. Please find a cdn of Angular 2 as well as copy related stylesheet files.

<style>
    /* Attention: Some codes have been hidden here*/
    .transition{
        transition: all ease 0.25s;
        right: 0px;
    }
</style>

<body>
    <button (click)="hidden=!hidden">Hide</button>
    <div class="drawer" [ngClass]="{'transition':hidden}"></div>
    <p>Hidden: {{hidden}}</p>
</body>

In this example, we can figure out that in button directive, (click)="opened=!opened" means when we click on this button, Angular 2 will reverse the value of hidden.
Similarly, [ngClass]="{'transition':opened}" means when the value of opened is true, Angular 2 will apply the class transition to the div.

Apart from that, we also have [hidden]="expression" which means we would hide the element when expression is true.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值