【问题标题】:Angular2 ng2-bootstrap collapse add transition/animationAngular2 ng2-bootstrap 折叠添加过渡/动画
【发布时间】:2016-12-29 02:53:57
【问题描述】:

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

对此有已知的解决方案吗?

【问题讨论】:

    标签: angular collapse ng2-bootstrap


    【解决方案1】:

    动画即将在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

    【讨论】:

    • 当我们等待升级时,这对我有用!
    • 截至今天,有谁知道动画是否登陆 ng2-bootstrap ?实际上,更好的是,他们从哪个版本登陆(如果有的话)?无法从他们的问题github.com/valor-software/ng2-bootstrap/issues/355 中弄清楚。感谢任何可以照亮的人
    • 谢谢!没有collapseDirective 的替代方法效果很好!特别是对于那些不想被 angular bootstrap 束缚的人
    • @yurzui,第一个 Plunker 似乎不再工作了。
    猜你喜欢
    • 1970-01-01
    • 2022-07-07
    • 2012-07-15
    • 2018-05-22
    • 1970-01-01
    • 2015-01-29
    • 1970-01-01
    • 2018-09-03
    • 2015-07-22
    相关资源
    最近更新 更多