


js implements delayed loading of web page images to improve web page opening speed_javascript skills
There are many ways to improve the loading speed of web pages. Using jquery.lazyload.js to implement asynchronous lazy loading of images will be a good method to improve the speed of web page opening for websites with a lot of images on the page. On the left side of the column list on the Code Jun website, you can see an article thumbnail display module when previewing on the PC, which will prolong the loading time of the web page to a certain extent. This article uses the asynchronous delayed loading method of images to improve the page loading speed of this website.
Pictures are loaded asynchronously, which means that it is not necessary to load and display all the pictures on the page at once. When the user slides the scroll bar to a certain position, the picture at the corresponding position will be loaded and displayed. This can greatly improve the loading speed of the web page. Further improve user experience.
There are many technical articles with a lot of pictures. If all the pictures are required to be loaded at once when opening the web page, the user will definitely have to wait for a very long time. This approach will make the user experience very bad, and there is no need to load all the images on the page at once. Asynchronous lazy loading of images is the most reasonable and appropriate approach in web design.
We use jquery.lazyload.js to implement asynchronous lazy loading of images. Remember to load jQuery first.
1. Import JS plug-in:
<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="jquery.lazyload.js"></script>
2. Insert JavaScript code into the page:
$(document).ready(function($){ $("img").lazyload({ placeholder:"grey.gif", //加载图片前的占位图片 effect:"fadeIn" //加载图片使用的效果(淡入) }); });
Through the above two steps, you can easily implement asynchronous delayed loading of web page images.
Let me give you a detailed introduction:
Sometimes we see some large websites. If the page has many pictures, when you scroll to the corresponding row, the pictures in the current row are loaded immediately. In this way, when the page is opened, only the pictures in the visible area are added, and the other pictures are not added. Hidden images are not loaded, which speeds up page loading. This solution is better for longer pages.
Recommended: Use the jquery image lazy loading plug-in jquery.lazyload to achieve image delay
Implementation principle:
Change all images that need to be delayed loaded into the following format:
<img lazy_src="图片路径" border="0"/>
Then when the page loads, save all the images using lazy_src into an array, then calculate the top of the visible area when scrolling, and then make the top of the delayed-loaded images smaller than the current visible area (i.e. the image The src value of the image appearing in the visible area is replaced with lazy_src (load the image):
JS code:
lazyLoad = (function() { var map_element = {}; var element_obj = []; var download_count = 0; var last_offset = -1; var doc_body; var doc_element; var lazy_load_tag; function initVar(tags) { doc_body = document.body; doc_element = document.compatMode == 'BackCompat' ? doc_body : document.documentElement; lazy_load_tag = tags || ["img", "iframe"]; }; function initElementMap() { var all_element = []; //从所有相关元素中找出需要延时加载的元素 for (var i = 0, len = lazy_load_tag.length; i < len; i++) { var el = document.getElementsByTagName(lazy_load_tag[i]); for (var j = 0, len2 = el.length; j < len2; j++) { if (typeof (el[j]) == "object" && el[j].getAttribute("lazy_src")) { element_obj.push(all_element[key]); } } } for (var i = 0, len = element_obj.length; i < len; i++) { var o_img = element_obj[i]; var t_index = getAbsoluteTop(o_img); //得到图片相对document的距上距离 if (map_element[t_index]) { map_element[t_index].push(i); } else { //按距上距离保存一个队列 var t_array = []; t_array[0] = i; map_element[t_index] = t_array; download_count++; //需要延时加载的图片数量 } } }; function initDownloadListen() { if (!download_count) return; var offset = (window.MessageEvent && !document.getBoxObjectFor) ? doc_body.scrollTop : doc_element.scrollTop; //可视化区域的offtset=document的高+ var visio_offset = offset + doc_element.clientHeight; if (last_offset == visio_offset) { setTimeout(initDownloadListen, 200); return; } last_offset = visio_offset; var visio_height = doc_element.clientHeight; var img_show_height = visio_height + offset; for (var key in map_element) { if (img_show_height > key) { var t_o = map_element[key]; var img_vl = t_o.length; for (var l = 0; l < img_vl; l++) { element_obj[t_o[l]].src = element_obj[t_o[l]].getAttribute("lazy_src"); } delete map_element[key]; download_count--; } } setTimeout(initDownloadListen, 200); }; function getAbsoluteTop(element) { if (arguments.length != 1 || element == null) { return null; } var offsetTop = element.offsetTop; while (element = element.offsetParent) { offsetTop += element.offsetTop; } return offsetTop; } function init(tags) { initVar(tags); initElementMap(); initDownloadListen(); }; return { init: init } })();
How to use: Change the src of the image that needs to be delayed loaded on the page to lazy_src, then put the above js at the end of the body, and then call: lazyLoad.init();
The method of teasing can use firebug to check whether the temporary image is delayed loading.
Also:
If there is a column with content switching on your page, the images in the content may not be displayed when switching. The solution is to load the images separately during the content, such as:
///切换内容的代码… chlid.find("img[init_src]").each(function(){ $(this).attr("src",$(this).attr("init_src")); $(this).removeAttr("init_src"); });
The so-called asynchronous loading of images means that you don’t have to load all the images at once. You can call it delayed loading or buffered loading.
Let’s see if you have this need: there are a lot of pictures in an article. If you load all the pictures when loading the article, it will undoubtedly slow down the loading speed and make the user wait longer. Therefore, I want to find such an article. A plug-in that allows the web page to only load images within the browser's field of view. Images that do not appear within the range will not be loaded temporarily and will be loaded gradually when the user slides the scroll bar. lazyload is used to achieve this effect.
Lazyload.js is actually a plug-in for jQuery. Its full name is jquery.lazyload.js. You can tell its function just by looking at its name - it means lazy loading. Since it is written in javascript, it is suitable for all web pages, including WordPress.
If you want to use lazyload, you must first load jQuery, which relies on jQuery to achieve effects.
The above is the entire content of this article. I hope everyone has a deeper understanding of js to implement delayed loading of web page images.

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

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the

Matter.js is a 2D rigid body physics engine written in JavaScript. This library can help you easily simulate 2D physics in your browser. It provides many features, such as the ability to create rigid bodies and assign physical properties such as mass, area, or density. You can also simulate different types of collisions and forces, such as gravity friction. Matter.js supports all mainstream browsers. Additionally, it is suitable for mobile devices as it detects touches and is responsive. All of these features make it worth your time to learn how to use the engine, as this makes it easy to create a physics-based 2D game or simulation. In this tutorial, I will cover the basics of this library, including its installation and usage, and provide a

This article demonstrates how to automatically refresh a div's content every 5 seconds using jQuery and AJAX. The example fetches and displays the latest blog posts from an RSS feed, along with the last refresh timestamp. A loading image is optiona

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.
