简体   繁体   English

对于动画,Angular.JS延迟路线更改

[英]Angular.JS delay route change for do animation

I'm using Semantic UI as a front-end framework. 我正在使用Semantic UI作为前端框架。 In it, I've a Javascript function for create some transitions, more info Here . 在其中,我有一个Javascript函数用于创建一些过渡,更多信息在这里

I want use it for create route change animation in Angular.JS. 我想用它来在Angular.JS中创建路径更改动画。 I tried to do this: 我试着这样做:

app.run(['$location', '$rootScope', function($location, $rootScope) {
    $rootScope.$on('$routeChangeStart', function (event, next) {
        $("#main").transition('fade up', '300ms');
    });
    $rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
        $("#main").transition('fade down', '300ms');
    });
}])

#main is DOM element where is my ng-view. #main是DOM元素,其中是我的ng-view。 But it doesn't works propertly, because route change become too fast. 但它并不适用,因为路线变化太快了。 So, my question is, what can I delay route change? 所以,我的问题是,我可以延迟路线改变? Or maybe is a better solution? 或者可能是更好的解决方案?

I think thinking about delaying the route change isn't a great idea, as you may hit into problems if there are route changes in quick succession. 我认为考虑延迟路线改变并不是一个好主意,因为如果路线快速连续变化,你可能会遇到问题。

However, Angular has built in support for animations, including javascript-powered transitions like (presumably) the ones from Semantic UI. 然而,Angular已经内置了对动画的支持,包括javascript驱动的转换,如(可能)来自Semantic UI的转换。 There is a fair bit of information at 有相当多的信息

http://www.yearofmoo.com/2013/08/remastered-animation-in-angularjs-1-2.html#how-to-make-animations-in-angularjs http://www.yearofmoo.com/2013/08/remastered-animation-in-angularjs-1-2.html#how-to-make-animations-in-angularjs

But what I think you'll need is to 但我认为你需要的是

  • Add a class, like "my-animated-view", to the ng-view (/ui-view?) element 将一个类(如“my-animated-view”)添加到ng-view(/ ui-view?)元素中
  • Write code similar to the below (copied + modified from the above page), customising the "enter" transition to transition views coming in. If you want a transition on the views leaving, do it on the "leave". 编写类似于下面的代码(从上一页复制+修改),自定义“进入”过渡到过渡视图。如果你想要离开视图的过渡,请在“离开”上进行。 The others can be left as default (which just call the done() function to tell Angular the animation is done), as I don't think you need them in your case 其他可以保留为默认值(只需调用done()函数告诉Angular动画完成),因为我认为你不需要它们

What Angular will do is on entry of a view, it will call the "enter" transition, and clean up after you call the passed "done" function. Angular将在输入视图时执行什么操作,它将调用“enter”转换,并在调用传递的“done”函数后进行清理。

myApp.animation('.my-animated-view', function() {
  return {
    enter : function(element, done) {    
      //node the done method here as the 3rd param
      $(element).transition('fade up', '300ms', done);

      return function(cancelled) {
        /* this (optional) function is called when the animation is complete
           or when the animation has been cancelled (which is when
           another animation is started on the same element while the
           current animation is still in progress). */
        if(cancelled) {
          // Something here to stop?
        }
      }
    },

    leave : function(element, done) { done(); },
    move : function(element, done) { done(); },

    beforeAddClass : function(element, className, done) { done(); },
    addClass : function(element, className, done) { done(); },

    beforeRemoveClass : function(element, className, done) { done(); },
    removeClass : function(element, className, done) { done(); },

    allowCancel : function(element, event, className) {}
  };
});    

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

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