私は最近次のAngular 2 Read Moreコンポーネントを作成しました。このコンポーネントが果たす役割は、「続きを読む」と「読みにくい」リンクでテキストの長いブロックを崩壊させて拡大することです。文字カウントに基づくものではなく、指定された最大高さに基づいています。角2滑らかな上下のアニメーション
import { Component, Input, ElementRef, AfterViewInit } from '@angular/core';
@Component({
selector: 'read-more',
template: `
<div [innerHTML]="text" [class.collapsed]="isCollapsed" [style.height]="isCollapsed ? maxHeight+'px' : 'auto'">
</div>
<a *ngIf="isCollapsable" (click)="isCollapsed =! isCollapsed">Read {{isCollapsed? 'more':'less'}}</a>
`,
styles: [`
div.collapsed {
overflow: hidden;
}
`]
})
export class ReadMoreComponent implements AfterViewInit {
//the text that need to be put in the container
@Input() text: string;
//maximum height of the container
@Input() maxHeight: number = 100;
//set these to false to get the height of the expended container
public isCollapsed: boolean = false;
public isCollapsable: boolean = false;
constructor(private elementRef: ElementRef) {
}
ngAfterViewInit() {
let currentHeight = this.elementRef.nativeElement.getElementsByTagName('div')[0].offsetHeight;
//collapsable only if the contents make container exceed the max height
if (currentHeight > this.maxHeight) {
this.isCollapsed = true;
this.isCollapsable = true;
}
}
}
などに使用:コンポーネントがうまく機能
<read-more [text]="details" [maxHeight]="250"></read-more>
。ここでは、コンポーネントのスライドアップ/ダウンのアニメーションをいくつか追加して、Read Moreリンクをクリックするとコンテンツがスライドし、Less lessがクリックされるとコンテンツが指定された最大高さまでスライドするようにする必要があります。
誰でもこれを達成する方法を教えてください。
[私を参照してください。ここで答える](http://stackoverflow.com/a/37843393/1375316)。この例は、まさにあなたがやろうとしていることです。 – threeve
ありがとう、私は実際に上記のスタイル情報を上記のコンポーネントに追加しようとしましたが、これはうまくいかないようです。スタイル:[' div.collapsed { オーバーフロー:隠し; } '、 ' div { 移行:高さ500msの容易さ; } '] –
それがうまくいくなら、私の解決策を有効にしてもよろしいですか? – Julien