0

I'm new to angular animations, and i've run into a problem. i have a stacked dropdown menu, and when i click a menu item, i want it to be animated like it's sliding into view. i'm using bootstrap4, and the div is display: none, in it's first state, then display: block. I also have a directive, that handles opening and closing it, and tracks outside clicks. right now, i can only set the menu to slide open the first click, or every other (which gets out of sync if i click outside the button and the menu closed, i.e. the animation state is not changed)

is there a way to animate a div being created in angular? something like on entry and on exit, so i can get a two way animation going? here is my snippet:

animations: [
        trigger('menuSlide', [
            state('closed', style({
                'height': '1px',
            })),
            state('open', style({
                'height': '108px',
            })),
            transition('closed => open', animate(50)),
        ])
    ]
})
export class NetworkComponent implements OnInit {
    @ViewChild('DropdownOne') dropdownOne: ElementRef;
    stateOne = 'closed';
    stateTwo = 'closed';

    constructor() {}

    animate() {
        this.stateOne = 'open';
        console.log('open');
        this.normalState(this.stateOne);
    }

    normalState(state) {
        console.log('closed')
        state = closed;
    }

currently it only opens the first time animated, i have made it a switch statement, but again it goes out of sync with outside clicks. a directive handles the state

here is the directive just for reference:

// handles dropdown menu and click out function for multiple items

    constructor(private elRef: ElementRef) {}

    @HostListener('document:click', ['$event'])
    clickOut(event) {
        let clickedComponent = event.target;
        let inside = false;

        do {
                if (clickedComponent === this.elRef.nativeElement) {
                    inside = true;
                }
                clickedComponent = clickedComponent.parentNode;
        } while (clickedComponent);
        if (inside) {
            if (this.elRef.nativeElement.classList.contains('show')) {
                this.elRef.nativeElement.classList.remove('show');
            } else {
                this.elRef.nativeElement.classList.add('show');
            }
        } else {
            this.elRef.nativeElement.classList.remove('show');
        }
    }

}

--UPDATE-- currently i can animate the div by having it start at a height: 0px; then ending it's state at the height i want. but this is really messy, and when closing, it has a slight 'glitch' as it shrinks and then goes to display: none; again.

FussinHussin
  • 1,718
  • 4
  • 19
  • 39
  • Angular 4.2 introduced `Staggering` animation .. Please check this link https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwjph5vhy5XVAhWLG5QKHQVmDEEQFgglMAA&url=https%3A%2F%2Fcoursetro.com%2Fposts%2Fcode%2F78%2FCreating-Stagger-Animations-in-Angular-4&usg=AFQjCNEqGPM81ll9q6LLDDrAJ1ZO0Nyhtg – Ghanshyam Singh Jul 19 '17 at 14:49
  • thanks for the link, but this does not answer my question – FussinHussin Jul 19 '17 at 19:38

0 Answers0