var conf = {
'loadfirst': true,
'loadimg': true
};
for (var item in conf) {
if (item in co) {
conf.item = co.item;
}
}
_this.setting = {
"mobileHeight": 0, //扩展屏幕的高度,使第一屏加载个数可配置
"loadNum": 1 //滚动时,当前节点之后加载个数
};
$.extend(_this.setting, _this.getSetting());
_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));
}
});
}
};
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;
}
});
}
;(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);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有