Angular 2 Slide Up and Down Animation

后端 未结 4 1436
小鲜肉
小鲜肉 2020-12-15 09:54

I recently built the following Angular 2 Read More component. What this component does is collapse and expand long blocks of text with \"Read more\" and \"Read Less\" links.

4条回答
  •  死守一世寂寞
    2020-12-15 10:29

    This is what I use in Angular 8.1.2. The beauty of the code is that it supports unlimited height of the div that needs to be shown/collapsed and also makes smooth transitions.

    TS FILE:

    import {Component, OnInit} from '@angular/core';
    import {trigger, transition, animate, style, state} from '@angular/animations';
    
    @Component({
        selector: 'app-all-data',
        templateUrl: './all-data.page.html',
        styleUrls: ['./all-data.page.scss'],
        animations: [
            trigger('openClose', [
                state('open', style({
                    height: '*',
                    opacity: 1,
                })),
                state('closed', style({
                    height: '0',
                    opacity: 0
                })),
                transition('open => closed', [
                    animate('0.35s')
                ]),
                transition('closed => open', [
                    animate('0.35s')
                ]),
            ]),
        ]
    })
    export class AllDataPage implements OnInit {
    
        showCardBody = false;
    
        constructor() {
        }
    
        ngOnInit() {
        }
    
        /**
         * Toggle details on click
         */
        showDetails() {
            this.showCardBody = !this.showCardBody;
        }
    
    }
    

    HTML FILE:

    
    
    

    This is some content

    This is some content

    This is some content

    This is some content

    This is some content

提交回复
热议问题