Home >Web Front-end >JS Tutorial >How to implement image preloading and delayed loading with Jquery_jquery
The example in this article describes how Jquery implements image preloading and delayed loading. Share it with everyone for your reference. The specific analysis is as follows:
There are many projects that often need to determine whether to perform corresponding operations after the image is loaded, or to delay loading of the image. Although there are already very good plug-ins on the Internet, it always feels a bit awkward to have to load a separate plug-in for these effects. It’s so comfortable, I just wrote a method myself:
arr: It can be an array to store the image path, or it can be the jquery object of the selected img;
funLoading: The operation performed after each individual image is loaded;
funOnLoad: Operation after all images are loaded;
funOnError: Operation when a single image loading error occurs.
For example:
Lazy Load depends on jQuery. Please add the following code to the head area of the page:
Set sensitivity
JavaScript is enabled in almost all browsers. However, you may still want to display real images on clients that do not support JavaScript. To degrade gracefully when the browser does not support JavaScript, you can write real image fragments
Set sensitivity
By default, the image will be loaded when it appears on the screen. If you want to load the image in advance, you can set the threshold option. Set threshold to 200 to make the image load in advance when it is 200 pixels away from the screen.
Placeholder image
You can also set a placeholder image and define events to trigger the loading action. At this time, you need to set a URL address for the placeholder image. Transparent, gray and white 1x1 pixel images are already included in the plug-in.
Event triggered loading
The event can be any jQuery event, such as: click and mouseover. You can also use custom events, such as: sporty and foobar. By default, it is in a waiting state until the user scrolls to the position of the picture on the window. In gray To prevent the placeholder image from loading until it is clicked, you can do this:
When the image is fully loaded, the plug-in uses the show() method by default to display the image. In fact, you can use any special effects you want to process. The following code uses the FadeIn effect. This is the effect demonstration page.
You can use the plug-in on images in scrollable containers, such as DIV elements with scroll bars. All you have to do is define the container as a jQuery object and pass it as a parameter to the initialization method. This is a horizontal scrolling demo page and vertical scrolling demo page.
When scrolling the page, Lazy Load will loop through the loaded images. In the loop, it will detect whether the image is within the visible area. By default, the loop will stop when the first image that is not in the visible area is found. The image is considered Fluidly distributed, the order of the images in the page is the same as the order in the HTML code. But in some layouts, this assumption is not true. However, you can control the loading behavior through the failurelimit option.
Lazy loading of images
An incomplete function of the Lazy Load plug-in, but it can also be used to implement lazy loading of images. The following code implements loading after the page is loaded. 5 seconds after the page is loaded, the images in the specified area will Load automatically. This is the lazy loading demo page.
There may be many hidden pictures buried on your page. For example, if the plug-in is used to filter the list, you can continuously modify the display status of each item in the list. In order to improve performance, Lazy Load ignores hidden pictures by default. If you want to load hidden images, please set skip_invisible to false
I hope this article will be helpful to everyone’s JavaScript programming design.