简体   繁体   English

使用AngularJS摇动动画

[英]Shake animation with AngularJS

I'm still learning AngularJS and have previously been using jQuery etc. to archive same feeling. 我仍在学习AngularJS,并且以前一直在使用jQuery等来归档相同的感觉。

I have a login function which redirects a user if succesful login and otherwise - as for now - prints to the console. 我有一个登录功能,如果登录成功,则该用户将重定向用户,否则就象现在一样打印到控制台。 I would like to "shake" the form-html-DOM if password is incorrect. 如果密码不正确,我想“摇晃” form-html-DOM。

This is my code in my controller: 这是我在控制器中的代码:

    user.login(self.username, self.password).then(
        function(res) {
            $location.path('/dashboard');
        }, function(res) {
            console.log(res.data.error);
        }
    );

With jQuery I would do something like: 使用jQuery,我会做类似的事情:

    user.login(self.username, self.password).then(
        function(res) {
            $location.path('/dashboard');
        }, function(res) {
            $("#my-login-form").shake();
        }
    );

This is of course not good with AngularJS. 对于AngularJS,这当然不好。 What is the simplest and best way to do something similar? 做类似事情的最简单,最好的方法是什么? I've tried to read the ngAnimate docs, but need a better understanding/example of something like that. 我试图阅读ngAnimate文档,但需要更好的理解/类似的例子。

You could use CSS to add the animation. 您可以使用CSS添加动画。 For instance animate.css and then use angular to add the class when something happens. 例如animate.css ,然后在发生某些情况时使用angular添加类。

Example: 例:

View: 视图:

<div ng-class="{'shake':error}" style="width:200px;height:200px;background:blue" class="animated"></div>

Controller: 控制器:

user.login(self.username, self.password).then(
    function(res) {
        $location.path('/dashboard');
    }, function(res) {
        console.log(res.data.error);
        $scope.error = true;
        setTimeout(function(){ 
            $scope.error = false;
        }, 1000);
    }
);

Explanation: 说明:

ng-class="{'shake':error}" ng-class =“ {'shake':error}”

conditionally add the "shake" class if the error variable is true. 如果error变量为true,则有条件地添加“ shake”类。 Together with the "animated" the shake effect is triggered as defined in animated.css. 如“ animated”(动画)一样,将触发抖动效果,如animation.css中所定义。 You could of course create your own css styles as well. 当然,您也可以创建自己的CSS样式。

Demo : https://jsfiddle.net/mkraLxs3/3/ 演示https : //jsfiddle.net/mkraLxs3/3/

You will need to use ng-animate. 您将需要使用ng-animate。 Here you can find a good tutorial to start using it. 在这里,您可以找到一个很好的教程来开始使用它。 And here you can find a CSS3 animation to do the shake. 在这里,您可以找到CSS3动画来进行摇动。

You will have to: 你不得不:

  • Include angular-animate.js and ngAnimate to your angular module 将angular-animate.js和ngAnimate包含到您的angular模块中
  • Assign CSS class with animation to form 为CSS类分配动画以形成
  • When login returns an error change the CSS class's animation property so it will play the animation. 登录返回错误时,请更改CSS类的animation属性,以便它将播放动画。

You can find how to do points 1 and 3 in the first tutorial I sent to you and the css animation in the second one. 您可以在我发送给您的第一个教程中找到如何做点1和3,在第二个教程中找到css动画。

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

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