PerformanceElementTiming: naturalHeight 屬性
naturalHeight 是一個只讀屬性,屬於 PerformanceElementTiming 介面,它返回影像元素的固有高度。
值
一個無符號 32 位整數(unsigned long),如果該屬性應用於影像,則表示影像的固有高度;如果應用於文字,則為 0。
示例
記錄 naturalHeight
在此示例中,透過新增 elementtiming 屬性來觀察一個 <img> 元素。註冊了一個 PerformanceObserver 來獲取型別為 "element" 的所有效能條目,並使用 buffered 標誌來訪問觀察者建立之前的資料。該影像檔案的寬度為 1000 畫素,高度為 750 畫素。呼叫 entry.naturalHeight 返回 750,即以畫素為單位的固有高度。
html
<img
src="image.jpg"
alt="a nice image"
elementtiming="big-image"
id="myImage" />
js
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach((entry) => {
if (entry.identifier === "big-image") {
console.log(entry.naturalHeight);
}
});
});
observer.observe({ type: "element", buffered: true });
規範
| 規範 |
|---|
| Element Timing API # dom-performanceelementtiming-naturalheight |
瀏覽器相容性
載入中…