减少图片请求,分批加载图片
| var lazyLoad = { | |
| Init: function () { | |
| return $("img[lazyload]"); | |
| }, | |
| Calculate: function (lazyloadobject) { | |
| var windowHeight = $(window).height(); | |
| var arrReturn = {}; | |
| var _scrollTop; | |
| if (lazyloadobject.length == 0) { | |
| return null; | |
| } | |
| else { | |
| lazyloadobject.each(function (i) { | |
| _scrollTop = parseInt($(this).offset().top - windowHeight); | |
| if (!arrReturn.hasOwnProperty(_scrollTop)) { | |
| arrReturn[_scrollTop] = new Array(); | |
| } | |
| arrReturn[_scrollTop].push($(this)); | |
| }); | |
| this.ArrLoad = arrReturn; | |
| return arrReturn; | |
| } | |
| }, | |
| ArrLoad: null, | |
| IsLoad: function (scrolltop, objectstop) { | |
| if (objectstop != null && objectstop != {}) { | |
| for (i in this.ArrLoad) { | |
| if (parseInt(i) <= scrolltop && this.ArrLoad.hasOwnProperty(i)) { | |
| for (j = 0; j < this.ArrLoad[i].length; j++) { | |
| this.ArrLoad[i][j].attr("src", this.ArrLoad[i][j].attr("lazyload")).removeAttr("lazyload"); | |
| } | |
| delete this.ArrLoad[i]; | |
| } | |
| } | |
| } | |
| }, | |
| Run: function () { | |
| var lazyLoadObject = this.Init(); | |
| this.Calculate(lazyLoadObject); | |
| arrScrollTop = this.ArrLoad; | |
| if (arrScrollTop == null) { | |
| return false; | |
| } | |
| else { | |
| var _this = this; | |
| _this.IsLoad($(window).scrollTop(), arrScrollTop); | |
| $(window).scroll(function () { | |
| _this.IsLoad($(this).scrollTop(), arrScrollTop); | |
| }); | |
| } | |
| } | |
| } | |
| $(function () { | |
| lazyLoad.Run(); | |
| }); | |
| $(window).resize(function () { | |
| lazyLoad.Run(); | |
| }); |
相关推荐
云中舞步 2020-11-12
杨德龙 2020-11-11
JohnYork 2020-10-16
wangzhaotongalex 2020-09-22
xiaoseyihe 2020-11-16
Crazyshark 2020-11-13
K先生 2020-11-10
思君夜未眠 2020-09-04
点滴技术生活 2020-08-21
MaggieRose 2020-08-19
kevinweijc 2020-08-18
wintershii 2020-08-17
vapaad 2020-08-17
wera00 2020-08-17
移动开发与培训 2020-08-16
ReunionIsland 2020-08-16
JimyFengqi 2020-08-16