Determine image loading status and achieve percentage effect loading
Preface
Some large external resources will cause the page to load slowly. At this time, the loading effect is usually added; what is implemented here is the loading effect based on the percentage of the image loading progress
How about Determine the status of image loading
1. onload onerror
It is recommended to use this method. After the image is loaded successfully, the onload event will be triggered. Even if there is a cache, as long as the image address is requested again, it will be triggered. onload event; failure to load the image will trigger the onerror event. This method has good compatibility and is recommended. 2. imgObj.onreadystatechange
Some browsers support this method. According to readState === 'complete', you can determine whether the image is loaded.
Tested:
Chrome and Firefox will not trigger this event
IE Edge version will not trigger this event
IE 10 9 will trigger this event Event; lower versions have not been tested
so it is not recommended to use
3. imgObj.complete
The complete attribute can determine whether the image is loaded. However, different browsers handle complete inconsistently:
If the picture resource is normal, the picture is loaded successfully. All browsers change complete from false to true;
But if the picture resource is abnormal, the picture Chrome and Firefox change from false to true after loading failure; but IE will always be false
, so this method is not recommended.
Image resource loading progress
You can judge whether a single image resource has been loaded, and it is easy to calculate the loading progress of all image resources on the entire page.
document.addEventListener('DOMContentLoaded', function(){ var imgs = document.querySelectorAll('img'), //所有图片资源 show = 0, //百分比 num = 0; //加载完成的个数 var all = imgs.length; [].slice.call(imgs).forEach(function(element,index){ //不管是否加载成功,都num+1 element.addEventListener('load',function(){ num++; show = Math.floor(100*num/all); }) element.addEventListener('error',function(){ num++; show = Math.floor(100*num/all); }) }) })
With the addition of mask and percentage words, it is easy to achieve the loading percentage effect.
After all
, hide the mask to achieve a more friendly loading effect
window.onload = function(){ document.querySelector('.mask').classList.add('hide'); }
The above is the detailed content of Determine image loading status and achieve percentage effect loading. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

Momo, a well-known social platform, provides users with a wealth of functional services for their daily social interactions. On Momo, users can easily share their life status, make friends, chat, etc. Among them, the setting status function allows users to show their current mood and status to others, thereby attracting more people's attention and communication. So how to set your own Momo status? The following will give you a detailed introduction! How to set status on Momo? 1. Open Momo, click More in the lower right corner, find and click Daily Status. 2. Select the status. 3. The setting status will be displayed.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

Question: How to determine whether the date is the previous day in Go language? In daily development, we often encounter situations where we need to determine whether the date is the previous day. In the Go language, we can implement this function through time calculation. The following will be combined with specific code examples to demonstrate how to determine whether the date is the previous day in Go language. First, we need to import the time package in the Go language. The code is as follows: import("time") Then, we define a function IsYest

jQuery is a JavaScript library widely used in web development. It provides many simple and convenient methods to operate web page elements and handle events. In actual development, we often encounter situations where we need to determine whether a variable is empty. This article will introduce several common methods of using jQuery to determine whether a variable is empty, and attach specific code examples. Method 1: Use the if statement to determine varstr="";if(str){co
