6

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

有没有已知的解决方案?

4

1 回答 1

11

动画即将推出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 示例

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 示例

于 2016-09-11T13:52:22.230 回答