


Simple implementation of javascript delayed loading technology (lazyload)_javascript skills
1. Foreword
Lazy loading technology (referred to as lazyload) is not a new technology. It is a solution for js programmers to optimize web page performance. The core of lazyload is on-demand loading. Lazyload can be found in large websites. For example, Google's image search page, Thunder homepage, Taobao, QQ space, etc. Therefore, mastering lazyload technology is a good choice. Unfortunately, the jquery plug-in lazy load official website (http://www.appelsiini.net/projects/lazyload) says it does not support it. New version of browser.
2. In what situations is lazyload more suitable?
It involves pictures, flash resources, iframes, web page editors (similar to FCK), etc., which take up a lot of bandwidth, and these modules are not available in browsers for the time being. Within the viewport, you can use lazyload to load this type of resource at the appropriate time. Avoid loading too many resources when the web page is opened and making users wait too long.
3. How to implement lazyload?
Difficulties of lazyload How to load the resources that the user needs at the appropriate time (the resources that the user needs here refer to the resources that are presented in the browser's visible area). Therefore, we need to know several pieces of information to determine whether the target has been presented in the client area, including:
1. The position of the visual area relative to the top of the browser
2. The position of the resource to be loaded relative to the top of the browser.
After obtaining the above two points of data, you can use the following function to determine whether an object is in the browser's visible area.
// Return the browser’s visible area position
function getClient(){
var l,t,w,h;
l = document.documentElement.scrollLeft || document.body.scrollLeft;
t = document.documentElement.scrollTop || document.body.scrollTop;
w = document.documentElement.clientWidth; .documentElement.clientHeight;
Return {'left':l,'top':t,'width':w,'height':h} ;
}
//Return to the location of the resource to be loaded
function getSubClient(p){
var l = 0,t = 0,w,h;
w = p.offsetWidth;
h = p.offsetHeight;
while(p.offsetParent ){
l = p.offsetLeft;
t = p.offsetTop;
p = p.offsetParent; 'width':w,'height':h } ;
}
The function getClient() returns the browser client area area information, and getSubClient() returns the target module area information. At this time, determining whether the target module appears in the client area is actually determining whether the above two rectangles intersect.
lc1 = rec1.left rec1.width / 2;
lc2 = rec2.left rec2.width / 2;
tc1 = rec1.top rec1.height / 2;
tc2 = rec2.top rec2 .height / 2;
w1 = (rec1.width rec2.width) / 2;
h1 = (rec1.height rec2.height) / 2;
return Math.abs(lc1 - lc2) < ; w1 && Math.abs(tc1 - tc2) }
Now we can basically implement delayed loading. Next, we write some in the window.onscroll event The code monitors whether the target area is rendered in the client area.
var d1 = document.getElementById("d1");
Window.onscroll = function(){
var prec1 = getClient();
var prec2 = getSubClient(d1);
if(intens(prec1,prec2)){
alert("true")
}
팝업 창에 필요한 리소스만 로드하면 됩니다.
여기서 주목해야 할 점은 대상 개체가 클라이언트 영역에 표시되면 팝업 창이 다음과 같이 계속해서 팝업된다는 점입니다. 따라서 먼저 팝업을 띄워야 합니다. 창 이후 이 영역의 모니터링을 취소하려면 여기서 배열을 사용하여 모니터링해야 할 개체를 수집합니다. 또한 onscroll 이벤트와 onresize 이벤트는 브라우저의 가시 영역 정보를 변경하므로 이러한 유형의 이벤트가 트리거된 후 대상 개체가 가시 영역에 있는지 다시 계산해야 합니다. () 함수는 이를 달성하기 위해 사용됩니다.(Thunder 홈페이지의 Lazyload 대상 객체가 브라우저의 가시 영역에 있는지 여부는 onresize 이벤트에서 다시 계산되지 않습니다. 따라서 먼저 브라우저 창을 특정 크기로 줄이면 , 이미지를 로드해야 하는 영역으로 스크롤한 다음 최대화를 클릭하면 이미지가 로드되지 않습니다. 하하, 나중에 필요합니다.
요소 추가:
// 브라우저 영역에 특정 하위 영역이 있는지 비교
function jiance(arr,prec1,callback){
var prec2;
for(var i = arr.length - 1 ; i >= 0 ;i--){
if(arr[i]) {
prec2 = getSubClient(arr[i] );
if(intens(prec1,prec2)){
콜백(arr[i]);
모니터링 삭제
delete arr[i] = getClient(); arr,prec1,function(obj){
//리소스 로드...
Alert(obj.innerHTML)
})
}
//하위 영역 1
var d1 = document.getElementById("d1");
//하위 영역 2개
var d2 = document.getElementById("d2")
//눌러야 함 영역 컬렉션을 로드해야 함
var arr = [d1,d2];
window.onscroll = function(){
// 다시 계산
autocheck();
}
window.onresize = function(){
/ / Recalculate
autocheck();
이제 팝업 창에 필요한 리소스만 로드하면 됩니다. 필요하신 분이나 질문이 있으신 분은 저에게 연락주세요.

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)
