Lazy Load Plugin for jQuery
可实现图片出现在视口时才加载的效果,网上的例子一堆堆。
官方网站:http://www.appelsiini.net/projects/lazyload
网上例子个人觉得比较好的:http://www.neoease.com/lazy-load-jquery-plugin-delay-load-image/
使用起来难度不大,配置一下效果就有了(网上有说这个插件的异步加载有bug,不过貌似新版本已经修复),下面是源码:
1 /* 2 * Lazy Load - jQuery plugin for lazy loading images 3 * 4 * Copyright (c) 2007-2013 Mika Tuupola 5 * 6 * Licensed under the MIT license: 7 * http://www.opensource.org/licenses/mit-license.php 8 * 9 * Project home: 10 * http://www.appelsiini.net/projects/lazyload 11 * 12 * Version: 1.9.3 13 * 14 */ 15 16 (function($, window, document, undefined) { 17 var $window = $(window); 18 19 $.fn.lazyload = function(options) { 20 var elements = this; 21 var $container; 22 var settings = { 23 threshold : 0, 24 failure_limit : 0, 25 event : "scroll", 26 effect : "show", 27 container : window, 28 data_attribute : "original", 29 skip_invisible : true, 30 appear : null, 31 load : null, 32 placeholder : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" 33 }; 34 35 function update() { 36 var counter = 0; 37 38 elements.each(function() { 39 var $this = $(this); 40 if (settings.skip_invisible && !$this.is(":visible")) { 41 return; 42 } 43 if ($.abovethetop(this, settings) || 44 $.leftofbegin(this, settings)) { 45 /* Nothing. */ 46 } else if (!$.belowthefold(this, settings) && 47 !$.rightoffold(this, settings)) { 48 $this.trigger("appear"); 49 /* if we found an image we'll load, reset the counter */ 50 counter = 0; 51 } else { 52 if (++counter > settings.failure_limit) { 53 return false; 54 } 55 } 56 }); 57 58 } 59 60 if(options) { 61 /* Maintain BC for a couple of versions. */ 62 if (undefined !== options.failurelimit) { 63 options.failure_limit = options.failurelimit; 64 delete options.failurelimit; 65 } 66 if (undefined !== options.effectspeed) { 67 options.effect_speed = options.effectspeed; 68 delete options.effectspeed; 69 } 70 71 $.extend(settings, options); 72 } 73 74 /* Cache container as jQuery as object. */ 75 $container = (settings.container === undefined || 76 settings.container === window) ? $window : $(settings.container); 77 78 /* Fire one scroll event per scroll. Not one scroll event per image. */ 79 if (0 === settings.event.indexOf("scroll")) { 80 $container.bind(settings.event, function() { 81 return update(); 82 }); 83 } 84 85 this.each(function() { 86 var self = this; 87 var $self = $(self); 88 89 self.loaded = false; 90 91 /* If no src attribute given use data:uri. */ 92 if ($self.attr("src") === undefined || $self.attr("src") === false) { 93 if ($self.is("img")) { 94 $self.attr("src", settings.placeholder); 95 } 96 } 97 98 /* When appear is triggered load original image. */ 99 $self.one("appear", function() { 100 if (!this.loaded) { 101 if (settings.appear) { 102 var elements_left = elements.length; 103 settings.appear.call(self, elements_left, settings); 104 } 105 $("<img />") 106 .bind("load", function() { 107 108 var original = $self.attr("data-" + settings.data_attribute); 109 $self.hide(); 110 if ($self.is("img")) { 111 $self.attr("src", original); 112 } else { 113 $self.css("background-image", "url('" + original + "')"); 114 } 115 $self[settings.effect](settings.effect_speed); 116 117 self.loaded = true; 118 119 /* Remove image from array so it is not looped next time. */ 120 var temp = $.grep(elements, function(element) { 121 return !element.loaded; 122 }); 123 elements = $(temp); 124 125 if (settings.load) { 126 var elements_left = elements.length; 127 settings.load.call(self, elements_left, settings); 128 } 129 }) 130 .attr("src", $self.attr("data-" + settings.data_attribute)); 131 } 132 }); 133 134 /* When wanted event is triggered load original image */ 135 /* by triggering appear. */ 136 if (0 !== settings.event.indexOf("scroll")) { 137 $self.bind(settings.event, function() { 138 if (!self.loaded) { 139 $self.trigger("appear"); 140 } 141 }); 142 } 143 }); 144 145 /* Check if something appears when window is resized. */ 146 $window.bind("resize", function() { 147 update(); 148 }); 149 150 /* With IOS5 force loading images when navigating with back button. */ 151 /* Non optimal workaround. 兼容移动端*/
152 if ((/(?:iphone|ipod|ipad).*os 5/gi).test(navigator.appVersion)) { 153 $window.bind("pageshow", function(event) { 154 if (event.originalEvent && event.originalEvent.persisted) { 155 elements.each(function() { 156 $(this).trigger("appear"); 157 }); 158 } 159 }); 160 } 161 162 /* Force initial check if images should appear. */ 163 $(document).ready(function() { 164 update(); 165 }); 166 167 return this; 168 }; 169 170 /* Convenience methods in jQuery namespace. */ 171 /* Use as $.belowthefold(element, {threshold : 100, container : window}) */ 172 //判断图片是否在视口中 173 $.belowthefold = function(element, settings) { 174 var fold; 175 176 if (settings.container === undefined || settings.container === window) { 177 fold = (window.innerHeight ? window.innerHeight : $window.height()) + $window.scrollTop(); 178 } else { 179 fold = $(settings.container).offset().top + $(settings.container).height(); 180 } 181 182 return fold <= $(element).offset().top - settings.threshold; 183 }; 184 185 $.rightoffold = function(element, settings) { 186 var fold; 187 188 if (settings.container === undefined || settings.container === window) { 189 fold = $window.width() + $window.scrollLeft(); 190 } else { 191 fold = $(settings.container).offset().left + $(settings.container).width(); 192 } 193 194 return fold <= $(element).offset().left - settings.threshold; 195 }; 196 197 $.abovethetop = function(element, settings) { 198 var fold; 199 200 if (settings.container === undefined || settings.container === window) { 201 fold = $window.scrollTop(); 202 } else { 203 fold = $(settings.container).offset().top; 204 } 205 206 return fold >= $(element).offset().top + settings.threshold + $(element).height(); 207 }; 208 209 $.leftofbegin = function(element, settings) { 210 var fold; 211 212 if (settings.container === undefined || settings.container === window) { 213 fold = $window.scrollLeft(); 214 } else { 215 fold = $(settings.container).offset().left; 216 } 217 218 return fold >= $(element).offset().left + settings.threshold + $(element).width(); 219 }; 220 221 $.inviewport = function(element, settings) { 222 return !$.rightoffold(element, settings) && !$.leftofbegin(element, settings) && 223 !$.belowthefold(element, settings) && !$.abovethetop(element, settings); 224 }; 225 226 /* Custom selectors for your convenience. */ 227 /* Use as $("img:below-the-fold").something() or */ 228 /* $("img").filter(":below-the-fold").something() which is faster */ 229 230 $.extend($.expr[":"], { 231 "below-the-fold" : function(a) { return $.belowthefold(a, {threshold : 0}); }, 232 "above-the-top" : function(a) { return !$.belowthefold(a, {threshold : 0}); }, 233 "right-of-screen": function(a) { return $.rightoffold(a, {threshold : 0}); }, 234 "left-of-screen" : function(a) { return !$.rightoffold(a, {threshold : 0}); }, 235 "in-viewport" : function(a) { return $.inviewport(a, {threshold : 0}); }, 236 /* Maintain BC for couple of versions. */ 237 "above-the-fold" : function(a) { return !$.belowthefold(a, {threshold : 0}); }, 238 "right-of-fold" : function(a) { return $.rightoffold(a, {threshold : 0}); }, 239 "left-of-fold" : function(a) { return !$.rightoffold(a, {threshold : 0}); } 240 }); 241 242 })(jQuery, window, document);
使用原生JS实现延迟加载的一个小例子,转自:http://js.fgm.cc/learn/lesson10/07.html,思路跟上面那个差不多,不过容易看懂的多。。。
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>延时加载</title> 6 <style type="text/css"> 7 body,div,ul,li{margin:0;padding:0;} 8 #box{width:770px;margin:0 auto;padding:10px 0 0 10px;background:#f00;overflow:hidden;zoom:1;} 9 #box li{float:left;width:375px;height:225px;color:#fff;padding:10px 0;display:inline;text-align:center;margin:0 10px 10px 0;background:#000 url(img/lazy/loading.gif) 50% 50% no-repeat;} 10 #box li img.loaded{width:353px;height:225px;vertical-align:middle;} 11 </style> 12 </head> 13 <script type="text/javascript"> 14 var fgm = { 15 on: function(element, type, handler) { 16 return element.addEventListener ? element.addEventListener(type, handler, false) : element.attachEvent("on" + type, handler) 17 }, 18 bind: function(object, handler) { 19 return function() { 20 return handler.apply(object, arguments) 21 } 22 }, 23 pageX: function(element) { 24 return element.offsetLeft + (element.offsetParent ? arguments.callee(element.offsetParent) : 0) 25 }, 26 pageY: function(element) { 27 return element.offsetTop + (element.offsetParent ? arguments.callee(element.offsetParent) : 0) 28 }, 29 hasClass: function(element, className) { 30 return new RegExp("(^|\s)" + className + "(\s|$)").test(element.className) 31 }, 32 attr: function(element, attr, value) { 33 if(arguments.length == 2) { 34 return element.attributes[attr] ? element.attributes[attr].nodeValue : undefined 35 } 36 else if(arguments.length == 3) { 37 element.setAttribute(attr, value) 38 } 39 } 40 }; 41 //延时加载 42 function LazyLoad(obj) { 43 this.lazy = typeof obj === "string" ? document.getElementById(obj) : obj; 44 this.aImg = this.lazy.getElementsByTagName("img"); 45 this.fnLoad = fgm.bind(this, this.load); 46 this.load(); 47 fgm.on(window, "scroll", this.fnLoad); 48 fgm.on(window, "resize", this.fnLoad); 49 } 50 LazyLoad.prototype = { 51 load: function() { 52 var iScrollTop = document.documentElement.scrollTop || document.body.scrollTop; 53 var iClientHeight = document.documentElement.clientHeight + iScrollTop; 54 var i = 0; 55 var aParent = []; 56 var oParent = null; 57 var iTop = 0; 58 var iBottom = 0; 59 var aNotLoaded = this.loaded(0); 60 if(this.loaded(1).length != this.aImg.length) { 61 for(i = 0; i < aNotLoaded.length; i++) { 62 oParent = aNotLoaded[i].parentElement || aNotLoaded[i].parentNode; 63 iTop = fgm.pageY(oParent); 64 iBottom = iTop + oParent.offsetHeight; 65 if((iTop > iScrollTop && iTop < iClientHeight) || (iBottom > iScrollTop && iBottom < iClientHeight)) { 66 aNotLoaded[i].src = fgm.attr(aNotLoaded[i], "data-img") || aNotLoaded[i].src; 67 aNotLoaded[i].className = "loaded"; 68 } 69 } 70 } 71 }, 72 loaded: function(status) { 73 var array = []; 74 var i = 0; 75 for(i = 0; i < this.aImg.length; i++) 76 eval("fgm.hasClass(this.aImg[i], "loaded")" + (!!status ? "&&" : "||") + "array.push(this.aImg[i])"); 77 return array 78 } 79 }; 80 //应用 81 fgm.on(window, "load", function () {new LazyLoad("box")}); 82 </script> 83 <body> 84 <ul id="box"> 85 <li><img src="img/lazy/none.gif" data-img="img/lazy/1.jpg" /></li> 86 <li><img src="img/lazy/none.gif" data-img="img/lazy/2.jpg" /></li> 87 <li><img src="img/lazy/none.gif" data-img="img/lazy/3.jpg" /></li> 88 <li><img src="img/lazy/none.gif" data-img="img/lazy/4.jpg" /></li> 89 <li><img src="img/lazy/none.gif" data-img="img/lazy/5.jpg" /></li> 90 <li><img src="img/lazy/none.gif" data-img="img/lazy/6.jpg" /></li> 91 <li><img src="img/lazy/none.gif" data-img="img/lazy/7.jpg" /></li> 92 <li><img src="img/lazy/none.gif" data-img="img/lazy/8.jpg" /></li> 93 <li><img src="img/lazy/none.gif" data-img="img/lazy/9.jpg" /></li> 94 <li><img src="img/lazy/none.gif" data-img="img/lazy/10.jpg" /></li> 95 <li><img src="img/lazy/none.gif" data-img="img/lazy/11.jpg" /></li> 96 <li><img src="img/lazy/none.gif" data-img="img/lazy/12.jpg" /></li> 97 <li><img src="img/lazy/none.gif" data-img="img/lazy/13.jpg" /></li> 98 <li><img src="img/lazy/none.gif" data-img="img/lazy/14.jpg" /></li> 99 <li><img src="img/lazy/none.gif" data-img="img/lazy/15.jpg" /></li> 100 <li><img src="img/lazy/none.gif" data-img="img/lazy/16.jpg" /></li> 101 <li><img src="img/lazy/none.gif" data-img="img/lazy/17.jpg" /></li> 102 <li><img src="img/lazy/none.gif" data-img="img/lazy/18.jpg" /></li> 103 <li><img src="img/lazy/none.gif" data-img="img/lazy/19.jpg" /></li> 104 <li><img src="img/lazy/none.gif" data-img="img/lazy/20.jpg" /></li> 105 <li><img src="img/lazy/none.gif" data-img="img/lazy/21.jpg" /></li> 106 <li><img src="img/lazy/none.gif" data-img="img/lazy/22.jpg" /></li> 107 <li><img src="img/lazy/none.gif" data-img="img/lazy/23.jpg" /></li> 108 <li><img src="img/lazy/none.gif" data-img="img/lazy/24.jpg" /></li> 109 <li><img src="img/lazy/none.gif" data-img="img/lazy/25.jpg" /></li> 110 <li><img src="img/lazy/none.gif" data-img="img/lazy/26.jpg" /></li> 111 <li><img src="img/lazy/none.gif" data-img="img/lazy/27.jpg" /></li> 112 <li><img src="img/lazy/none.gif" data-img="img/lazy/28.jpg" /></li> 113 <li><img src="img/lazy/none.gif" data-img="img/lazy/29.jpg" /></li> 114 <li><img src="img/lazy/none.gif" data-img="img/lazy/30.jpg" /></li> 115 </ul> 116 </body> 117 </html>