Home  >  Article  >  Web Front-end  >  The principle and implementation of jquery's lazy loading

The principle and implementation of jquery's lazy loading

php中世界最好的语言
php中世界最好的语言Original
2018-03-09 15:19:562515browse

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;
    }
}

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);
    }
})

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层
        }
    }
})

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!

Statement:
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