簡體   English   中英

如何在 Angular 中獲取圖像的實際大小並調整大小?

[英]How to get real size of image and resized in Angular?

有一張圖:

@ViewChild('image')
readonly image: ElementRef;

Html:

 <img #image class="image" />

CSS自動調整后如何獲得實際尺寸(寬度,高度)和調整大小?

我試過這個:

  console.log(this.container.nativeElement.clientHeight);
  console.log(this.container.nativeElement.clientWidth);

它返回我 500 和 0。

嘗試這個:

<img #image src="images/path" class="image" (load)="onLoad()" />

在 controller

onLoad() {
  console.log((this.image.nativeElement as HTMLImageElement).width);
  console.log((this.image.nativeElement as HTMLImageElement).height);
}

演示: https://stackblitz.com/edit/angular-rqceml?file=src/app/app.component.ts

你可以試試這個:

 console.log((this.container.nativeElement as HTMLImageElement).naturalWidth); console.log((this.container.nativeElement as HTMLImageElement).naturalHeight);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM