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