Home > Web Front-end > JS Tutorial > body text

The principle and implementation of jquery's lazy loading

php中世界最好的语言
Release: 2018-03-09 15:19:56
Original
2472 people have browsed it

This time I will bring you the principle and implementation of jquery's lazy loading. What are the things to note when implementing jquery's lazy loading? The following is a practical case, let's take a look.

The principle of lazy loading

1. Code implementation, load into the current viewport:

function isVisible($node){    var winH = $(window).height(),
        scrollTop = $(window).scrollTop(),
        offSetTop = $(window).offSet().top;    if (offSetTop < winH + scrollTop) {        return true;
    } else {        return false;
    }
}
Copy after login

2. Then add the browser's

eventlistening Function, let the browser check whether the element appears within the visible range of the window every time it scrolls:

$(window).on("scroll", function{    if (isVisible($node)){        console.log(true);
    }
})
Copy after login

3. Come on, Pikachu!

var hasShowed = false;
$(window).on("sroll",function{    if (hasShowed) {        return;
    } else {        if (isVisible($node)) {
            hasShowed = !hasShowed;            console.log(true);            //ajax请求刷新view层
        }
    }
})
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to the php Chinese website

Other related articles!

Related reading:

Problems with VUE-CLI @2.9.1 and later versions

How to customize user settings for WebStorm

The above is the detailed content of The principle and implementation of jquery's lazy loading. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!