Home Web Front-end JS Tutorial js A thing about loading images onload_javascript skills

js A thing about loading images onload_javascript skills

May 16, 2016 pm 05:16 PM
load

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:
 

Copy the code The code is as follows :

$(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:

Copy code The code is as follows:

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:

Copy code The code is as follows:

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:

Copy code The code is as follows:

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. . .

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Error loading plugin in Illustrator [Fixed] Error loading plugin in Illustrator [Fixed] Feb 19, 2024 pm 12:00 PM

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

Stremio subtitles not working; error loading subtitles Stremio subtitles not working; error loading subtitles Feb 24, 2024 am 09:50 AM

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

Outlook freezes when inserting hyperlink Outlook freezes when inserting hyperlink Feb 19, 2024 pm 03:00 PM

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.

PHP implements infinite scroll loading PHP implements infinite scroll loading Jun 22, 2023 am 08:30 AM

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

How to solve the problem that css cannot be loaded How to solve the problem that css cannot be loaded Oct 20, 2023 am 11:29 AM

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.

What should I do if Windows 7 fails to load the USB driver? What should I do if Windows 7 fails to load the USB driver? Jul 11, 2023 am 08:13 AM

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

A Complete Guide to Implementing Infinite Scroll Loading with Vue.js A Complete Guide to Implementing Infinite Scroll Loading with Vue.js Jun 09, 2023 pm 04:11 PM

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 to use JavaScript to achieve the infinite scroll effect of automatically loading when scrolling to the bottom of the page? How to use JavaScript to achieve the infinite scroll effect of automatically loading when scrolling to the bottom of the page? Oct 27, 2023 pm 06:30 PM

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

See all articles