When we browse large shopping websites such as Taobao and JD.com, many pictures often appear, and the pictures cost us a lot of traffic.
How do we solve this problem when writing web pages? At this time, we need to use the problem of lazy loading of our images, which not only solves the traffic problem, but also improves performance and so on.
Let’s take a look at the problem with lazy loading of images.
<span style="font-size: 18px"><br><span style="font-size: 18px"></span><br><span style="font-size: 18px"></span><br><span style="font-size: 18px"> <meta charset="UTF-8"></span><br><span style="font-size: 18px"> <title>Title</title></span><br><span style="font-size: 18px"> <style></style></span><br><span style="font-size: 18px"> *{</span><br><span style="font-size: 18px"> margin: 0;</span><br><span style="font-size: 18px"> padding: 0;</span><br><span style="font-size: 18px"> list-style-type: none;</span><br><span style="font-size: 18px"> }</span><br><span style="font-size: 18px"> html,body{</span><br><span style="font-size: 18px"> width: 100%;</span><br><span style="font-size: 18px"> height: 100%;</span><br><span style="font-size: 18px"> }</span><br><span style="font-size: 18px"> #div div{</span><br><span style="font-size: 18px"> width: 400px;</span><br><span style="font-size: 18px"> height: 500px;</span><br><span style="font-size: 18px"> /*border:1px solid #000;*/</span><br><span style="font-size: 18px"> display: inline-block;</span><br><span style="font-size: 18px"> margin:10px;</span><br><span style="font-size: 18px"> }</span><br><span style="font-size: 18px"> img{</span><br><span style="font-size: 18px"> width: 100%;</span><br><span style="font-size: 18px"> height: 100%;</span><br><span style="font-size: 18px"> }</span><br><span style="font-size: 18px"> </span><br><span style="font-size: 18px"> <script></script></span><br><span style="font-size: 18px"> // 获取距离父级距离</span><br><span style="font-size: 18px"> function getPos(obj) {</span><br><span style="font-size: 18px"> var t=0;//先声明两个变量 用来存储距离上面的高度 距离左边的高度</span><br><span style="font-size: 18px"> var l=0;</span><br><span style="font-size: 18px"> while (obj){//循环判断这个元素是否存在,存在才执行这个方法</span><br><span style="font-size: 18px"> t+=obj.offsetTop; // 加上距离父级的距离</span><br><span style="font-size: 18px"> l+=obj.offsetLeft;</span><br><span style="font-size: 18px"> obj=obj.offsetParent;// 有父级让他变成他的父级</span><br><span style="font-size: 18px"> }</span><br><span style="font-size: 18px"> return {left:l,top:t}; //把值传出去</span><br><span style="font-size: 18px"> }</span><br><br><span style="font-size: 18px"> window.onload=window.onscroll=function () {//滚动的时候和页面加载时执行的一样</span><br><span style="font-size: 18px"> var aImg=document.getElementsByTagName('img');//获取所有的元素img元素</span><br><span style="font-size: 18px"> var oSCrollT=document.body.scrollTop || document.documentElement.scrollTop;// 滚动条滚动高度</span><br><span style="font-size: 18px"> var oClh=document.documentElement.clientHeight;//可视区的高度</span><br><span style="font-size: 18px"> var oScr=oSCrollT + oClh; // 滚动条高度 + 可视区的高度</span><br><span style="font-size: 18px"> for(var i=0;i<aimg.length><br><span style="font-size: 18px"> if(oScr > getPos(aImg[i]).top){</span><br><span style="font-size: 18px"> aImg[i].src=aImg[i].getAttribute('_src'); //给src属性赋值</span><br><span style="font-size: 18px"> }</span><br><span style="font-size: 18px"> }</span><br><span style="font-size: 18px"> }</span><br><span style="font-size: 18px"> </span><br><span style="font-size: 18px"></span><br><span style="font-size: 18px"></span><br><span style="font-size: 18px"> <div id="div">
<br><span style="font-size: 18px"> <div><img src="/static/imghwm/default1.png" data-src="img/1.jpg" class="lazy" _ alt="Lazy loading problem of images" ></div></span><br><span style="max-width:90%"> <div><img src="/static/imghwm/default1.png" data-src="img/2.jpg" class="lazy" _ alt="Lazy loading problem of images" ></div></span><br><span style="max-width:90%"> <div><img src="/static/imghwm/default1.png" data-src="img/3.jpg" class="lazy" _ alt="Lazy loading problem of images" ></div></span><br><span style="max-width:90%"> <div><img src="/static/imghwm/default1.png" data-src="img/4.jpg" class="lazy" _ alt="Lazy loading problem of images" ></div></span><br><span style="max-width:90%"> <div><img src="/static/imghwm/default1.png" data-src="img/5.jpg" class="lazy" _ alt="Lazy loading problem of images" ></div></span><br><span style="max-width:90%"> <div><img src="/static/imghwm/default1.png" data-src="img/6.jpg" class="lazy" _ alt="Lazy loading problem of images" ></div></span><br><span style="max-width:90%"> <div><img src="/static/imghwm/default1.png" data-src="img/7.jpg" class="lazy" _ alt="Lazy loading problem of images" ></div></span><br><span style="max-width:90%"> <div><img src="/static/imghwm/default1.png" data-src="img/8.jpg" class="lazy" _ alt="Lazy loading problem of images" ></div></span><br><span style="max-width:90%"> <div><img src="/static/imghwm/default1.png" data-src="img/9.jpg" class="lazy" _ alt="Lazy loading problem of images" ></div></span><br><span style="max-width:90%"> <div><img src="/static/imghwm/default1.png" data-src="img/10.jpg" class="lazy" _ alt="Lazy loading problem of images" ></div></span><br><span style="max-width:90%"> <div><img src="/static/imghwm/default1.png" data-src="img/11.jpg" class="lazy" _ alt="Lazy loading problem of images" ></div></span><br><span style="max-width:90%"> <div><img src="/static/imghwm/default1.png" data-src="img/12.jpg" class="lazy" _ alt="Lazy loading problem of images" ></div></span><br><span style="max-width:90%"> <div><img src="/static/imghwm/default1.png" data-src="img/13.jpg" class="lazy" _ alt="Lazy loading problem of images" ></div></span><br><span style="max-width:90%"> <div><img src="/static/imghwm/default1.png" data-src="img/14.jpg" class="lazy" _ alt="Lazy loading problem of images" ></div></span><br><span style="max-width:90%"> <div><img src="/static/imghwm/default1.png" data-src="img/15.jpg" class="lazy" _ alt="Lazy loading problem of images" ></div></span><br><span style="max-width:90%"> </span>
</div></span><br><span style="font-size: 18px"></span><br><span style="font-size: 18px"><br>今天的内容就到这里,你学会了吗?</span></aimg.length></span></span>The above is the detailed content of Lazy loading problem of images. For more information, please follow other related articles on the PHP Chinese website!
The Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AMThe future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.
HTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AMThe roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.
HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AMHTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.
HTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AMHTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.
From Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AMHTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.
Understanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AMWebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.
The Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AMThe role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.
HTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AMHTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version






