I'm trying to animate a div using ng-show with CSS3 transitions. There are 2 divs: One in the foreground and another hidden behind it. The hidden div should slide up when the user mouse's over the foreground div. It should also remain shown if the users cursor moves over the slide div. If the users cursor leaves the slide div or the foreground div, it should slide back down. It doesn't seem to work correctly though. Here's a plunker: http://plnkr.co/edit/C1RaDTOXwpEMECGMkt19?p=preview
Is the css correct?
div#cover {
background-color: lightblue;
width: 90px;
height: 30px;
position:absolute;
bottom: 0px;
z-index:10;
}
.slide {
background-color: white;
width: 90px;
height: 30px;
position:absolute;
bottom: 0px;
z-index:5;
}
.slide.ng-hide-add, .slide.ng-hide-remove {
transition: bottom 1s ease;
}
.slide.ng-hide-remove-active {
bottom: 30px
}
.slide.ng-hide-add-active {
bottom: 0px
}
Any ideas?
You only need two states:
Let the base
slide
class define the state for shown (30px from bottom):When the expression used in the
ng-show
directive is evaluated to false the hidden state will be activated and the classng-hide
will be added to the element by Angular.Use that class to decorate the base class and define the hidden state (0px from bottom):
This means the two states will have the following classes when active:
slide
slide ng-hide
As you see the
slide
class is always present, which is why thetransition
is put there.You must use
!important
on thetransition
otherwise if you move the cursor back and forth over the element Angular will override it withtransition: none
briefly, which will cause the transition to interrupt and the element to snap into its final position.You must also use
display: block !important
in theslide
class to override the default behavior ofng-hide
, which isdisplay: none
.Demo: http://plnkr.co/edit/WN8v2riSYhhMyOgbZiWO?p=preview
Note: Personally I wouldn't recommend using
ng-mouseenter
orng-mouseleave
for this. Everytime they trigger the entire digest cycle and all watchers will be executed multiple times. The alternative would be to create a directive that uses event listeners to add/remove classes manually.