javascript瀑布流式图片懒加载实例
作者:bea
最近项目使用到了“懒加载”,现在更新一般,因为平时主要使移动端的开发所以库文件使用的是zepto.js 。当然也可以和jQuery 通用。 代码如下: /** * Created by zhiqiang on 2015/10/14. * hpuhouzhiqiang@gmail.com * 图片的懒加载 **/function loadImgLazy(node) { var lazyNode = $('[node-type=imglazy]', node), mobile
最近项目使用到了“懒加载”,现在更新一般,因为平时主要使移动端的开发所以库文件使用的是zepto.js 。当然也可以和jQuery 通用。
代码如下:
/**
* Created by zhiqiang on 2015/10/14.
* hpuhouzhiqiang@gmail.com
* 图片的懒加载
**/
function loadImgLazy(node) {
var lazyNode = $('[node-type=imglazy]', node),
mobileHeight, lazyOffSetHeight, tempHeight, currentNodeTop, imgObject,
imgDataSrc, localUrl;
localUrl = location.href;
// 获取当前浏览器可视区域的高度
mobileHeight = $(window).height();
return function(co) {
var conf = {
'loadfirst': true,
'loadimg': true
};
for (var item in conf) {
if (item in co) {
conf[item] = co[item];
}
}
var that = {};
var _this = {};
/**
* [replaceImgSrc 动态替换节点中的src]
* @param {[type]} tempObject [description]
* @return {[type]} [description]
*/
_this.replaceImgSrc = function(tempObject) {
var srcValue;
$.each(tempObject, function(i, item) {
imgObject = $(item).find('img[data-lazysrc]');
imgObject.each(function(i) {
imgDataSrc = $(this).attr('data-lazysrc');
srcValue = $(this).attr('src');
if (srcValue == '#') {
if (imgDataSrc) {
$(this).attr('src', imgDataSrc);
$(this).removeAttr('data-lazysrc');
}
}
});
});
};
/**
* 首屏判断屏幕上是否出现imglazy节点,有的话就加载图片
* @param {[type]} i) { currentNodeTop [description]
* @return {[type]} [description]
*/
_this.loadFirstScreen = function() {
if (conf.loadfirst) {
lazyNode.each(function(i) {
currentNodeTop = $(this).offset().top;
if (currentNodeTop < mobileHeight + 800) {
_this.replaceImgSrc($(this));
}
});
}
};
//当加载过首屏以后按照队列加载图片
_this.loadImg = function() {
if (conf.loadimg) {
$(window).on('scroll', function() {
var imgLazyList = $('[node-type=imglazy]', node);
for (var i = 0; i < 5; i++) {
_this.replaceImgSrc(imgLazyList.eq(i));
}
});
}
};
that = {
replaceImgSrc: _this.replaceImgSrc(),
mobileHeight: mobileHeight,
objIsEmpty: function(obj) {
for (var item in obj) {
return false;
}
return true;
},
destory: function() {
if (_this) {
$.each(_this, function(i, item) {
if (item && item.destory) {
item.destory();
}
});
_this = null;
}
$(window).off('scroll');
}
};
return that;
};
}
希望本文对大家学习javascript图片懒加载有所帮助。
有用 | 无用
代码如下:
/**
* Created by zhiqiang on 2015/10/14.
* hpuhouzhiqiang@gmail.com
* 图片的懒加载
**/
function loadImgLazy(node) {
var lazyNode = $('[node-type=imglazy]', node),
mobileHeight, lazyOffSetHeight, tempHeight, currentNodeTop, imgObject,
imgDataSrc, localUrl;
localUrl = location.href;
// 获取当前浏览器可视区域的高度
mobileHeight = $(window).height();
return function(co) {
var conf = {
'loadfirst': true,
'loadimg': true
};
for (var item in conf) {
if (item in co) {
conf[item] = co[item];
}
}
var that = {};
var _this = {};
/**
* [replaceImgSrc 动态替换节点中的src]
* @param {[type]} tempObject [description]
* @return {[type]} [description]
*/
_this.replaceImgSrc = function(tempObject) {
var srcValue;
$.each(tempObject, function(i, item) {
imgObject = $(item).find('img[data-lazysrc]');
imgObject.each(function(i) {
imgDataSrc = $(this).attr('data-lazysrc');
srcValue = $(this).attr('src');
if (srcValue == '#') {
if (imgDataSrc) {
$(this).attr('src', imgDataSrc);
$(this).removeAttr('data-lazysrc');
}
}
});
});
};
/**
* 首屏判断屏幕上是否出现imglazy节点,有的话就加载图片
* @param {[type]} i) { currentNodeTop [description]
* @return {[type]} [description]
*/
_this.loadFirstScreen = function() {
if (conf.loadfirst) {
lazyNode.each(function(i) {
currentNodeTop = $(this).offset().top;
if (currentNodeTop < mobileHeight + 800) {
_this.replaceImgSrc($(this));
}
});
}
};
//当加载过首屏以后按照队列加载图片
_this.loadImg = function() {
if (conf.loadimg) {
$(window).on('scroll', function() {
var imgLazyList = $('[node-type=imglazy]', node);
for (var i = 0; i < 5; i++) {
_this.replaceImgSrc(imgLazyList.eq(i));
}
});
}
};
that = {
replaceImgSrc: _this.replaceImgSrc(),
mobileHeight: mobileHeight,
objIsEmpty: function(obj) {
for (var item in obj) {
return false;
}
return true;
},
destory: function() {
if (_this) {
$.each(_this, function(i, item) {
if (item && item.destory) {
item.destory();
}
});
_this = null;
}
$(window).off('scroll');
}
};
return that;
};
}
希望本文对大家学习javascript图片懒加载有所帮助。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- JavaScript设计模式经典之工厂模式
- JavaScript的设计模式经典之建造者模式
- JavaScript的设计模式经典之代理模式
- jQuery实现验证年龄简单思路
- JavaScript实现99乘法表及隔行变色实例代码
- JavaScript中Object.prototype.toString方法的原理
- JavaScript判断变量是否为数组的方法(Array)
- 浅析jQuery Ajax请求参数和返回数据的处理
- jQuery+formdata实现上传进度特效遇到的问题
- JQuery EasyUI的使用
- 使用jQuery监听DOM元素大小变化
- JavaScript中的闭包
- jQuery中判断对象是否存在的方法汇总
- jquery中键盘事件小结
- javascript实现九宫格相加数值相等
- Javascript类型转换的规则实例解析
- 理解Javascript图片预加载
- Bootstarp风格的toggle效果分享
- javascript瀑布流式图片懒加载实例解析与优化