From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../web/api/htmlimageelement/complete/index.html | 90 ++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 files/zh-cn/web/api/htmlimageelement/complete/index.html (limited to 'files/zh-cn/web/api/htmlimageelement/complete') diff --git a/files/zh-cn/web/api/htmlimageelement/complete/index.html b/files/zh-cn/web/api/htmlimageelement/complete/index.html new file mode 100644 index 0000000000..cff31934c9 --- /dev/null +++ b/files/zh-cn/web/api/htmlimageelement/complete/index.html @@ -0,0 +1,90 @@ +--- +title: HTMLImageElement.complete +slug: Web/API/HTMLImageElement/complete +translation_of: Web/API/HTMLImageElement/complete +--- +

{{APIRef("HTML DOM")}}

+ +

{{domxref("HTMLImageElement")}} 的只读属性 complete 是一个布尔值, 表示图片是否完全加载完成。

+ +

语法

+ +
let doneLoading = htmlImageElement.complete;
+ +

+ +

当图片完全加载完成时值为 true ; 否则,值为 false

+ +

以下任意一条为true则认为图片完全加载完成:

+ + + +

It's worth noting that due to the image potentially being received asynchronously, the value of complete may change while your script is running.

+ +

Example

+ +

Consider a photo library app that provides the ability to open images into a lightbox mode for improved viewing as well as editing of the image. These photos may be very large, so you don't want to wait for them to load, so your code uses async/await to load the images in the background.

+ +

But imagine that you have other code that needs to only run when the image has completed loading, such as a command that performs red-eye removal on the image in the lightbox. While ideally this command wouldn't even be executed if the image hasn't fully loaded, for improved reliability you want to check to ensure this is the case.

+ +

So the fixRedEyeCommand() function, which is called by the button that triggers red-eye removal, checks the value of the lightbox image's complete property before attempting to do its work. This is demonstrated in the code below.

+ +
let lightboxElem = document.querySelector("#lightbox");
+let lightboxImgElem = lightboxElem.querySelector("img");
+let lightboxControlsElem = lightboxElem.querySelector(".toolbar");
+
+async function loadImage(url, elem) {
+  return new Promise((resolve, reject) => {
+    elem.onload = () => resolve(elem);
+    elem.onerror = reject;
+    elem.src = src;
+  });
+}
+
+async function lightBox(url) {
+  lightboxElem.style.display = "block";
+  await loadImage("https://somesite.net/huge-image.jpg", lightboxImgElem);
+  lightboxControlsElem.disabled = false;
+}
+
+/* ... */
+
+function fixRedEyeCommand() {
+  if (lightboxElem.style.display === "block" && lightboxImgElem.complete) {
+    fixRedEye(lightboxImgElem);
+  } else {
+    /* can't start doing this until the image is fully loaded */
+  }
+}
+
+ +

Specifications

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-img-complete', 'HTMLImageElement.complete')}}{{Spec2('HTML DOM')}}
+ +

Browser compatibility

+ + + +

{{Compat("api.HTMLImageElement.complete")}}

-- cgit v1.2.3-54-g00ecf