Home >Web Front-end >JS Tutorial >JavaScript waterfall flow image lazy loading example analysis and optimization_javascript skills
I wrote an article about "lazy loading" of images before. When I was sorting out the files on the weekend, I probably looked at the code I wrote before and found that there are many places that can be optimized.
This article mainly combines the previous article "javascript waterfall flow image lazy loading example" to take a look at some knowledge about "lazy loading" of images.
The purpose of "lazy loading" of images:
Load images as needed, which means loading images for display when they need to be displayed, reducing the network bandwidth overhead of one-time loading.
Let’s look at a piece of code first:
var conf = { 'loadfirst': true, 'loadimg': true }; for (var item in conf) { if (item in co) { conf.item = co.item; } }
Here I mainly want to achieve the merger of user configuration and default configuration. Writing code like this is not very elegant. Now I use $.extend for optimization. The code is as follows:
_this.setting = { "mobileHeight": 0, //扩展屏幕的高度,使第一屏加载个数可配置 "loadNum": 1 //滚动时,当前节点之后加载个数 }; $.extend(_this.setting, _this.getSetting());
Here I will focus on the two newly added parameters mobileHeight and loadNum
mobileHeight is the height of the default client. The larger the value, the more images will be loaded on the first screen;
loadNum If the current node appears on the screen, several nodes after the current node can be loaded at one time, which can increase the loading speed of images;
My code was written like this before:
_this.loadFirstScreen = function() { if (conf.loadfirst) { lazyNode.each(function(i) { currentNodeTop = $(this).offset().top; //这里的800就是上面提到的mobileHeight if (currentNodeTop < mobileHeight + 800) { _this.replaceImgSrc($(this)); } }); } }; _this.loadImg = function() { if (conf.loadimg) { $(window).on('scroll', function() { var imgLazyList = $('[node-type=imglazy]', node); //这里的5就是上面提到的loadNum for (var i = 0; i < 5; i++) { _this.replaceImgSrc(imgLazyList.eq(i)); } }); } };
To optimize my current code according to the configurable idea, it looks like this:
loadFirstSrceen: function() { // 加载首屏 var _this = this; var currentNodeTop; var imgNodeList = _this.imgNode; $(imgNodeList).each(function() { currentNodeTop = $(this).offset().top; if (currentNodeTop < _this.mobileHeight() + _this.setting.mobileHeight) { _this.replaceImgSrc($(this)); } }); }, scrollLoadImg: function() { //滚动的时候加载图片 var _this = this; var currentNodeTop; var scrollTop = $('body').scrollTop(); var imgLazyList = $('[node-type=imglazy]'); $(imgLazyList).each(function() { currentNodeTop = $(this).offset().top; if (currentNodeTop - scrollTop < _this.mobileHeight()) { //加载当前节点后的规定个数节点 for (var i = 0, len = _this.setting.loadNum; i < len; i++) { _this.replaceImgSrc($(imgLazyList).eq(i)); } return false; } }); }
A more important aspect is to organize the current code structure according to the idea of writing plug-ins. The code is as follows:
;(function($) { var LoadImgLazy = function(imgNode) { var _this = this; _this.imgNode = imgNode; _this.setting = { "mobileHeight": 0, //扩展屏幕的高度,使第一屏加载个数可配置 "loadNum": 1 //滚动时,当前节点之后加载个数 }; $.extend(_this.setting, _this.getSetting()); _this.loadFirstSrceen(); $(window).on('scroll', function() { _this.scrollLoadImg(); }); }; LoadImgLazy.prototype = { mobileHeight: function() { return $(window).height(); }, loadFirstSrceen: function() { // 加载首屏 var _this = this; var currentNodeTop; var imgNodeList = _this.imgNode; $(imgNodeList).each(function() { currentNodeTop = $(this).offset().top; if (currentNodeTop < _this.mobileHeight() + _this.setting.mobileHeight) { _this.replaceImgSrc($(this)); } }); }, scrollLoadImg: function() { //滚动的时候加载图片 var _this = this; var currentNodeTop; var scrollTop = $('body').scrollTop(); var imgLazyList = $('[node-type=imglazy]'); $(imgLazyList).each(function() { currentNodeTop = $(this).offset().top; if (currentNodeTop - scrollTop < _this.mobileHeight()) { //加载当前节点后的规定个数节点 for (var i = 0, len = _this.setting.loadNum; i < len; i++) { _this.replaceImgSrc($(imgLazyList).eq(i)); } return false; } }); }, replaceImgSrc: function(loadImgNode) { //动态替换图片 var srcValue; var imgDataSrc; var _this = this; var imgUrlList = $(loadImgNode).find('img[data-lazysrc]'); if (imgUrlList.length > 0) { imgUrlList.each(function(i) { imgDataSrc = $(this).attr('data-lazysrc'); srcValue = $(this).attr('src'); if (srcValue === '#') { if (imgDataSrc) { $(this).attr('src', imgDataSrc); $(this).removeAttr('data-lazysrc'); } } }); //移除已经运行过懒加载节点的node-type 对性能提升 $(loadImgNode).removeAttr('node-type'); } }, getSetting: function() { var userSetting = $('[lazy-setting]').attr('lazy-setting'); if (userSetting && userSetting !== '') { return $.parseJSON(userSetting); } else { return {}; } }, destory: function() { //销毁方法区 $(window).off('scroll'); } }; LoadImgLazy.init = function(imgNode) { new this(imgNode); }; window.LoadImgLazy = LoadImgLazy; })(Zepto);
Through this article, I hope you will have a deeper understanding of JavaScript waterfall flow image lazy loading and learn how to optimize it. Thank you for reading.