js A thing about loading images onload_javascript skills
First let me clarify my purpose:
When the user enters the page, display the loading icon, wait for the largest image to load, and then opacity the image;
The function is very simple, but the trouble is, I didn’t fully complete it;
When doing it, I naturally thought of the following method for the first time:
$(function(){
$('.banner img').load(function(){
console.log('already loaded')
} );
});
As the old saying goes, if your self-confidence is too inflated, it will be a huge blow in the end. I thought this was enough, so I looked it over and showed it to the boss. The boss clicked it twice and said that it would not be loaded. Loading transfer It just wouldn't come out. I said it shouldn't come out, and then I tried it myself, and it turned out to be true.
Then I searched for information and found that it was said to be the cause of caching, because when loading, the browser will not trigger the load event due to the cache; to no avail; I made a cruel move:
window.onload=function(){
console .log('Loaded')
};
Hehe, if this doesn’t work, I’ll give you a thumbs up; then my self-confidence is shattered again, so I bring it to the boss, who says it’s okay. The next day, the customer calls and says that the page is not loading, and the loading keeps going on and on. ; I answered him speechlessly: It should be that your Internet speed is too slow. . . You were waiting, and after a while he replied: This won’t work. Although it came out, the wait has been too long; can the time be shortened a little;
Helpless, I can only compress the pictures in various ways. After compressing, he still felt that it was not ideal; the boss said that this is ok, and now the project is just thrown into the space, which is very slow. If he network The speed is slow, and the loading time is naturally slow.
The boss said that’s what he said, but the task was still not completed. I was racking my brains and accidentally saw a carousel image jq plug-in on a foreign website. I went down and looked at the source code, and then the following trick was born:
var oImg = $('.banner img:eq( 0)');
oImg.attr('src') '?' (new Date()).getTime();
oImg.load(function(){
console.log('Already Loading')
});
After testing, it is normal. This means that when entering the page, add a time to the address of the image, so that there will be no cache every time it is loaded; and only one image is loaded, and one image is loaded. I won’t care about anything else after that;
After correction, I uploaded it without telling the boss; I didn’t dare to be careless this time, so I kept testing, and this time I found that the problem was no longer a problem;
Because the image path will be different every time when the page is loaded, it will be loaded every time, and the time consumed is the same as the first load; doesn’t that mean that it needs to be loaded once after entering it?
I’m dizzy......
After dozens of attempts, I finally found a way to defeat all the above methods:
function imgloading(){
console.log( 'Already loaded')
}
//Page call

This can solve the problem of image caching, and the load event will still be triggered; although I don’t understand this in detail; but I heard from others that this is still because of the page loading;
As we all know, web pages are loaded from top to bottom; when loading to img, I cannot get the img element in ready; when the page continues to load, after passing img, the current img represents Loading is complete. Since it is loaded, I think it is all loaded. Is it still useful to introduce load?
It is not difficult to see from the above method that when the page is loaded into img, it encounters the onload method, and it knows that this image must be loaded before it appears.
Okay. . . I don’t know if any colleagues have encountered this kind of problem and found a more perfect solution than this. . If you have a more perfect way, please be sure to leave a message and let me know. Thank you very much. I always feel that there is a better way. . .

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


![Error loading plugin in Illustrator [Fixed]](https://img.php.cn/upload/article/000/465/014/170831522770626.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
When launching Adobe Illustrator, does a message about an error loading the plug-in pop up? Some Illustrator users have encountered this error when opening the application. The message is followed by a list of problematic plugins. This error message indicates that there is a problem with the installed plug-in, but it may also be caused by other reasons such as a damaged Visual C++ DLL file or a damaged preference file. If you encounter this error, we will guide you in this article to fix the problem, so continue reading below. Error loading plug-in in Illustrator If you receive an "Error loading plug-in" error message when trying to launch Adobe Illustrator, you can use the following: As an administrator

Subtitles not working on Stremio on your Windows PC? Some Stremio users reported that subtitles were not displayed in the videos. Many users reported encountering an error message that said "Error loading subtitles." Here is the full error message that appears with this error: An error occurred while loading subtitles Failed to load subtitles: This could be a problem with the plugin you are using or your network. As the error message says, it could be your internet connection that is causing the error. So please check your network connection and make sure your internet is working properly. Apart from this, there could be other reasons behind this error, including conflicting subtitles add-on, unsupported subtitles for specific video content, and outdated Stremio app. like

If you encounter freezing issues when inserting hyperlinks into Outlook, it may be due to unstable network connections, old Outlook versions, interference from antivirus software, or add-in conflicts. These factors may cause Outlook to fail to handle hyperlink operations properly. Fix Outlook freezes when inserting hyperlinks Use the following fixes to fix Outlook freezes when inserting hyperlinks: Check installed add-ins Update Outlook Temporarily disable your antivirus software and then try creating a new user profile Fix Office apps Program Uninstall and reinstall Office Let’s get started. 1] Check the installed add-ins. It may be that an add-in installed in Outlook is causing the problem.

With the development of the Internet, more and more web pages need to support scrolling loading, and infinite scrolling loading is one of them. It allows the page to continuously load new content, allowing users to browse the web more smoothly. In this article, we will introduce how to implement infinite scroll loading using PHP. 1. What is infinite scroll loading? Infinite scroll loading is a method of loading web content based on scroll bars. Its principle is that when the user scrolls to the bottom of the page, background data is asynchronously retrieved through AJAX to continuously load new content. This kind of loading method

The solutions to the problem that CSS cannot be loaded include checking the file path, checking the file content, clearing the browser cache, checking the server settings, using developer tools and checking the network connection. Detailed introduction: 1. Check the file path. First, please make sure the path of the CSS file is correct. If the CSS file is located in a different part or subdirectory of the website, you need to provide the correct path. If the CSS file is located in the root directory, the path should be direct. ; 2. Check the file content. If the path is correct, the problem may lie in the CSS file itself. Open the CSS file to check, etc.

When installing the win7 system, some netizens encountered a situation where loading the USB driver failed. The USB device could not be recognized in the new win7 system, and common USB flash drives, mice and other devices could not be used. So what should I do if the installation of win7 fails to load the USB driver? Let Xiaobai teach you how to solve the problem of failure to load the USB driver when installing win7. Method 1: 1. First, we turn on the computer and enter the computer system, and check the computer system version in the computer system. Confirm whether the version of the computer system is consistent with the version of the device driver. 2. After confirming the driver version, connect the USB device to the computer system. The computer system shows that the device cannot connect to the system. 3. On the connection information page, click the Help button to view the help information. 4. If the computer department

As the amount of data continues to increase, scrolling loading of web pages has gradually become an important part of the user experience. In this post, we will discuss a complete guide on how to implement infinite scroll loading using Vue.js. What is infinite scroll loading? Infinite scroll loading, also known as infinite scroll, is a web design technique used to add more content as the user scrolls to the bottom of the page. This technology is commonly used on blogs, social media, online stores and other websites that need to display content dynamically. Infinite scrolling is not the same as pagination. In traditional paging, users must

How does JavaScript achieve the infinite scroll effect of automatically loading when scrolling to the bottom of the page? The infinite scroll effect is one of the common features in modern web development. It can automatically load more content when scrolling to the bottom of the page, allowing users to obtain more data or resources without manually clicking buttons or links. In this article, we'll explore how to use JavaScript to achieve this functionality and provide specific code examples. To achieve the infinite scrolling effect of automatically loading when scrolling to the bottom of the page, it is mainly divided into the following
