How to implement a flip over effect using AngularJ

2019-03-09 05:45发布

What would be the best way to achieve a flip over effect using AngularJS animations?

I would like the flip over effect to occur on click. Every time it's clicked, it should flip over to the other side.

Ideally, I guess, I'm looking for a directive implementation that uses Angular animations.

8条回答
贪生不怕死
2楼-- · 2019-03-09 06:14

Disclaimer Based on @artur's answer https://stackoverflow.com/a/23139242/1319998 , but hopefully both simplified and made more flexible.

A custom directive is the way to go, one that can be used as:

<flip flip-side="{{side}}">
  <flip-front>
    Front side contents
  </flip-front>
  <flip-back>
    Rear contents
  </flip-back>
</flip>

I think it should have certain properties:

  • Programatically controlled by an attribute. In this case, a string that is equal to 'front' or 'back'

    <flip flip-side="{{side}}">....</flip>
    

    this would allow programmatic access via the surrounding scope.

  • Integrated with ngAnimate/$animate. Specifically, if ngAnimate is removed or disabled, the animation should not occur, but the reveal of the other side happen immediately. Using $animate.addClass/$animate.removeClass would achieve this, adding/removing a flip-visible class together with display:block and display:none styles to make sure the right side is visible/hidden when the animations are disabled.

    flip > flip-front, flip > flip-back {
      display: none;
    }
    flip > .flip-visible {
      display: block;
    }
    
  • Controlled by CSS, with defaults. So if you want to change the duration of the flip, it's a CSS, and not a Javascript, addition.

    So it will have a style sheet to add styles required for the various stages of $animate.addClass / $animate.removeClass CSS animations explained at Year of Moo and $animate docs . The class will be flip-visible, so the extra classes will be .flip-visible-add, .flip-visible-add-active, .flip-visible-remove, and .flip-visible-remove-active classes.

    The full set of styles can be seen at http://plnkr.co/edit/bbYbMhiURnm6FqC9patp?p=preview, but the main construction is of the form:

    .flip-visible-add {
      // Initial setup: time and initial, pre-animation, transform
    }
    .flip-visible-add.flip-visible-add-active {
      // Target transform
    }
    

Putting all this together, the directive is quite short:

app.directive("flip", function($animate) {
  return {
    restrict : "E",
    controller: function($scope, $element, $attrs) {
      var elements = {
        'front': $element.find('flip-front'),
        'back': $element.find('flip-back')
      };
      $attrs.$observe('flipSide', function(visibleSide) {
        visibleSide = visibleSide || 'front';
        var otherSide = visibleSide == 'front' ? 'back' : 'front';
        $animate.removeClass(elements[otherSide], 'flip-visible');
        $animate.addClass(elements[visibleSide], 'flip-visible');
      });
    }
  }
});

This can all be seen in an example, together with the stylesheets to make it all work, at http://plnkr.co/edit/bbYbMhiURnm6FqC9patp?p=preview

查看更多
冷血范
3楼-- · 2019-03-09 06:17

I would simply add / remove a class on click.

If you want to hook into the angular animation system then take a look at the $animate service, in particular add/remove/setClass(). The service is usually used in directives. You might want to create a directive that reacts on a click event and triggers the animation. You even get informed when the animation has completed.

Chances are that it's not worth it ;)

查看更多
登录 后发表回答