Angular2 ng2-Bootstrap 折叠添加过渡/动画

标签 angular ng2-bootstrap

我正在用这个 https://valor-software.com/ng2-bootstrap/#/collapse 我想在 height 0 - auto 上添加动画/过渡,但我们都知道你不能在 height auto 上添加过渡。 我想添加一个带有持续时间的 slideToggle 效果。 试图寻找超过 3 天的解决方案...

是否有已知的解决方案?

最佳答案

动画即将在 ng2-bootstrap 中推出。您只需要稍等一下。

1) 今天,您可以利用以下解决方法:

@Component({
  selector: 'my-app',
  template: `
       <button type="button" class="btn btn-primary"
           (click)="isCollapsed = !isCollapsed">Toggle collapse
      </button>
      <div (collapsed)="setHeight(el, 0)"
           (expanded)="setHeight(el, 0);setHeight(el, el.scrollHeight)"
           [collapse]="isCollapsed"
           class="card card-block card-header block"  #el>
        <div class="well well-lg">Some content</div>
      </div>
  `,
  styles: [`
    .block {
      display: block !important;
      overflow: hidden !important;
      -webkit-transition: height .5s;
      transition: height .5s;
    }
  `]
})
export class App {
  isCollapsed: boolean = true;

  constructor(private renderer: Renderer) { }

  setHeight(el, height) {
    this.renderer.setElementStyle(el, 'height', height + 'px');
  }
}

另见工作 Plunker Example

2) 如果没有 CollapseDirective 指令,你可以这样做

@Component({
  selector: 'my-app',
  template: `
    <button type="button" class="btn btn-primary"
      (click)="height = height ? 0 : el.scrollHeight">Toggle collapse
    </button>

    <div       
      class="card card-block card-header block" [style.height]="height + 'px'" #el> 
      <div class="well well-lg">Some content</div>
    </div>
  `,
  styles: [`
    .block {
      overflow: hidden;
      -webkit-transition: height .5s;
      transition: height .5s;
    }
  `]
})
export class App {
  height = 0;
}

Plunker Example

关于Angular2 ng2-Bootstrap 折叠添加过渡/动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39081496/

相关文章:

javascript - 使用 *ngFor 时, Angular 事件绑定(bind)不起作用

angular2 错误 : this. $modal.modal 不是函数

angular - 如何在 Angular 2 中使用 ng2-bootstrap?

angular - 在哪里导入(导出?)使用 forRoot 的模块?

javascript - 标签 &lt;script&gt; 内的代码在 VScode、Angular 的 component.html 文件中不起作用

javascript - Angular,如何在多个页面而不是在一个大列表中显示 *ngFor

angular - 使用带有 ng-file-upload 的cropper

Angular 2同步Http服务

从父组件调用的子组件内的 Angular 2 ng2-bootstrap 模态

Angular 2 : get image from file upload