Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit df24410

Browse files
committed
fix(ngAnimate): force use of ng-anchor instead of a suffixed -anchor CSS class when triggering anchor animations
This fix changes anchored animations in ngAnimate to not append a series of CSS classes with a `-suffix` prefix to the anchor element. Use the `ng-anchor` instead CSS class instead. BREAKING CHANGE: Prior to this fix there were to ways to apply CSS animation code to an anchor animation. With this fix, the suffixed CSS -anchor classes are now not used anymore for CSS anchor animations. Instead just use the `ng-anchor` CSS class like so: ```html <div class="container-animation" ng-if="on"> <div ng-animate-ref="1" class="my-anchor-element"></div> </div> <div class="container-animation" ng-if="!on"> <div ng-animate-ref="1" class="my-anchor-element"></div> </div> ``` **before**: ```css /* before (notice the container-animation CSS class) */ .container-animation-anchor { transition:0.5s linear all; } ``` **now**: ```css /* now (just use `ng-anchor` on a class that both the elements that contain `ng-animate-ref` share) */ .my-anchor-element.ng-anchor { transition:0.5s linear all; } ```
1 parent e6d053d commit df24410

File tree

2 files changed

+51
-28
lines changed

2 files changed

+51
-28
lines changed

src/ngAnimate/animateCssDriver.js

-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var $$AnimateCssDriverProvider = ['$$animationProvider', function($$animationPro
55

66
var NG_ANIMATE_SHIM_CLASS_NAME = 'ng-animate-shim';
77
var NG_ANIMATE_ANCHOR_CLASS_NAME = 'ng-anchor';
8-
var NG_ANIMATE_ANCHOR_SUFFIX = '-anchor';
98

109
var NG_OUT_ANCHOR_CLASS_NAME = 'ng-anchor-out';
1110
var NG_IN_ANCHOR_CLASS_NAME = 'ng-anchor-in';
@@ -46,13 +45,11 @@ var $$AnimateCssDriverProvider = ['$$animationProvider', function($$animationPro
4645
function prepareAnchoredAnimation(classes, outAnchor, inAnchor) {
4746
var clone = jqLite(getDomNode(outAnchor).cloneNode(true));
4847
var startingClasses = filterCssClasses(getClassVal(clone));
49-
var anchorClasses = pendClasses(classes, NG_ANIMATE_ANCHOR_SUFFIX);
5048

5149
outAnchor.addClass(NG_ANIMATE_SHIM_CLASS_NAME);
5250
inAnchor.addClass(NG_ANIMATE_SHIM_CLASS_NAME);
5351

5452
clone.addClass(NG_ANIMATE_ANCHOR_CLASS_NAME);
55-
clone.addClass(anchorClasses);
5653

5754
rootBodyElement.append(clone);
5855

src/ngAnimate/module.js

+51-25
Original file line numberDiff line numberDiff line change
@@ -428,65 +428,91 @@
428428
* called `ng-animate-ref`.
429429
*
430430
* Let's say for example we have two views that are managed by `ng-view` and we want to show
431-
* that there is a relationship between two components situated in different views. By using the
431+
* that there is a relationship between two components situated in within these views. By using the
432432
* `ng-animate-ref` attribute we can identify that the two components are paired together and we
433433
* can then attach an animation, which is triggered when the view changes.
434434
*
435+
* Say for example we have the following template code:
436+
*
435437
* ```html
436438
* <!-- index.html -->
437439
* <div ng-view class="view-animation">
438440
* </div>
439441
*
440442
* <!-- home.html -->
441443
* <a href="#/banner-page">
442-
* <img src="./banner.jpg" ng-animate-ref="banner">
444+
* <img src="./banner.jpg" class="banner" ng-animate-ref="banner">
443445
* </a>
444446
*
445447
* <!-- banner-page.html -->
446-
* <img src="./banner.jpg" ng-animate-ref="banner">
448+
* <img src="./banner.jpg" class="banner" ng-animate-ref="banner">
447449
* ```
448450
*
449451
* Now, when the view changes (once the link is clicked), ngAnimate will examine the
450452
* HTML contents to see if there is a match reference between any components in the view
451-
* that is leaving and the view that is entering. It will then attempt to trigger a CSS
452-
* animation on the `.view-animation-anchor` CSS class (notice how `.view-animation` is
453-
* a shared CSS class on the ng-view element? This means that view-animation will apply to
454-
* both the enter and leave animations).
453+
* that is leaving and the view that is entering. It will scan both the view which is being
454+
* removed (leave) and inserted (enter) to see if there are any paired DOM elements that
455+
* contain a matching ref value.
455456
*
456-
* The two images match since they share the same ref value. ngAnimate will now apply a
457-
* suffixed version of each of the shared CSS classes with `-anchor`. Therefore we will
458-
* have a shared class of `view-animation-anchor` which we can use to setup our transition animation.
457+
* The two images match since they share the same ref value. ngAnimate will now create a
458+
* transport element (which is a clone of the first image element) and it will then attempt
459+
* to animate to the position of the second image element in the next view. For the animation to
460+
* work a special CSS class called `ng-anchor` will be added to the transported element.
459461
*
460-
* We can now attach a transition onto the `.view-animation-anchor` CSS class and then
462+
* We can now attach a transition onto the `.banner.ng-anchor` CSS class and then
461463
* ngAnimate will handle the entire transition for us as well as the addition and removal of
462464
* any changes of CSS classes between the elements:
463465
*
464466
* ```css
465-
* .view-animation-anchor {
467+
* .banner.ng-anchor {
466468
* /&#42; this animation will last for 1 second since there are
467469
* two phases to the animation (an `in` and an `out` phase) &#42;/
468470
* transition:0.5s linear all;
469471
* }
470472
* ```
471473
*
472-
* There are two stages for an anchor animation: `out` and `in`. The `out` stage happens first and that
473-
* is when the element is animated away from its origin. Once that animation is over then the `in` stage
474-
* occurs which animates the element to its destination. The reason why there are two animations is to
475-
* give enough time for the enter animation on the new element to be ready.
474+
* We also **must** include animations for the views that are being entered and removed
475+
* (otherwise anchoring wouldn't be possible since the new view would be inserted right away).
476+
*
477+
* ```css
478+
* .view-animation.ng-enter, .view-animation.ng-leave {
479+
* transition:0.5s linear all;
480+
* position:fixed;
481+
* left:0;
482+
* top:0;
483+
* width:100%;
484+
* }
485+
* .view-animation.ng-enter {
486+
* transform:translateX(100%);
487+
* }
488+
* .view-animation.ng-leave,
489+
* .view-animation.ng-enter.ng-enter-active {
490+
* transform:translateX(0%);
491+
* }
492+
* .view-animation.ng-leave.ng-leave-active {
493+
* transform:translateX(-100%);
494+
* }
495+
* ```
496+
*
497+
* Now we can jump back to the anchor animation. When the animation happens, there are two stages that occur:
498+
* an `out` and an `in` stage. The `out` stage happens first and that is when the element is animated away
499+
* from its origin. Once that animation is over then the `in` stage occurs which animates the
500+
* element to its destination. The reason why there are two animations is to give enough time
501+
* for the enter animation on the new element to be ready.
476502
*
477503
* The example above sets up a transition for both the in and out phases, but we can also target the out or
478504
* in phases directly via `ng-anchor-out` and `ng-anchor-in`.
479505
*
480506
* ```css
481-
* .view-animation-anchor.ng-anchor-out {
507+
* .banner.ng-anchor-out {
482508
* transition: 0.5s linear all;
483509
*
484510
* /&#42; the scale will be applied during the out animation,
485511
* but will be animated away when the in animation runs &#42;/
486512
* transform: scale(1.2);
487513
* }
488514
*
489-
* .view-animation-anchor.ng-anchor-in {
515+
* .banner.ng-anchor-in {
490516
* transition: 1s linear all;
491517
* }
492518
* ```
@@ -580,21 +606,21 @@
580606
width:100%;
581607
min-height:500px;
582608
}
583-
.view.ng-enter {
609+
.view.ng-enter, .view.ng-leave,
610+
.record.ng-anchor {
584611
transition:0.5s linear all;
612+
}
613+
.view.ng-enter {
585614
transform:translateX(100%);
586615
}
587-
.view.ng-enter.ng-enter-active {
616+
.view.ng-enter.ng-enter-active, .view.ng-leave {
588617
transform:translateX(0%);
589618
}
590-
.view.ng-leave {
591-
transition:0.5s linear all;
592-
}
593619
.view.ng-leave.ng-leave-active {
594620
transform:translateX(-100%);
595621
}
596-
.view-anchor {
597-
transition:0.5s linear all;
622+
.record.ng-anchor-out {
623+
background:red;
598624
}
599625
</file>
600626
</example>

0 commit comments

Comments
 (0)