正文转自:http://www.cnblogs.com/wscdzl/archive/2012/09/03/2668838.html

原理:通过 jQuery 插件 lazyload
使得在浏览器可知区域外的图形不会被加载,当图片被用户滚动到浏览器可见区域时图片才进行加载,有点类似利用
Google搜索图片时的成效。很理解,通过运用图片延时加载能够抓牢页面包车型地铁加载速度。

金镶玉裹福禄双全进程:

第3是引入jquery文件和插件文件。jquery文件在此间小编就不再赘述了。下边作者贴出插件文件,大家将该文件命名字为jquery.lazyload.js。

jQuery 1😉

  1 // JavaScript Document
  2 /*
  3  * Lazy Load - jQuery plugin for lazy loading images
  4  *
  5  * Copyright (c) 2007-2012 Mika Tuupola
  6  *
  7  * Licensed under the MIT license:
  8  *   http://www.opensource.org/licenses/mit-license.php
  9  *
 10  * Project home:
 11  *   http://www.appelsiini.net/projects/lazyload
 12  *
 13  * Version:  1.8.0
 14  *
 15  */
 16 (function($, window) {
 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         };
 33 
 34         function update() {
 35             var counter = 0;
 36       
 37             elements.each(function() {
 38                 var $this = $(this);
 39                 if (settings.skip_invisible && !$this.is(":visible")) {
 40                     return;
 41                 }
 42                 if ($.abovethetop(this, settings) ||
 43                     $.leftofbegin(this, settings)) {
 44                         /* Nothing. */
 45                 } else if (!$.belowthefold(this, settings) &&
 46                     !$.rightoffold(this, settings)) {
 47                         $this.trigger("appear");
 48                 } else {
 49                     if (++counter > settings.failure_limit) {
 50                         return false;
 51                     }
 52                 }
 53             });
 54 
 55         }
 56 
 57         if(options) {
 58             /* Maintain BC for a couple of versions. */
 59             if (undefined !== options.failurelimit) {
 60                 options.failure_limit = options.failurelimit; 
 61                 delete options.failurelimit;
 62             }
 63             if (undefined !== options.effectspeed) {
 64                 options.effect_speed = options.effectspeed; 
 65                 delete options.effectspeed;
 66             }
 67 
 68             $.extend(settings, options);
 69         }
 70 
 71         /* Cache container as jQuery as object. */
 72         $container = (settings.container === undefined ||
 73                       settings.container === window) ? $window : $(settings.container);
 74 
 75         /* Fire one scroll event per scroll. Not one scroll event per image. */
 76         if (0 === settings.event.indexOf("scroll")) {
 77             $container.bind(settings.event, function(event) {
 78                 return update();
 79             });
 80         }
 81 
 82         this.each(function() {
 83             var self = this;
 84             var $self = $(self);
 85 
 86             self.loaded = false;
 87 
 88             /* When appear is triggered load original image. */
 89             $self.one("appear", function() {
 90                 if (!this.loaded) {
 91                     if (settings.appear) {
 92                         var elements_left = elements.length;
 93                         settings.appear.call(self, elements_left, settings);
 94                     }
 95                     $("<img />")
 96                         .bind("load", function() {
 97                             $self
 98                                 .hide()
 99                                 .attr("src", $self.data(settings.data_attribute))
100                                 [settings.effect](settings.effect_speed);
101                             self.loaded = true;
102 
103                             /* Remove image from array so it is not looped next time. */
104                             var temp = $.grep(elements, function(element) {
105                                 return !element.loaded;
106                             });
107                             elements = $(temp);
108 
109                             if (settings.load) {
110                                 var elements_left = elements.length;
111                                 settings.load.call(self, elements_left, settings);
112                             }
113                         })
114                         .attr("src", $self.data(settings.data_attribute));
115                 }
116             });
117 
118             /* When wanted event is triggered load original image */
119             /* by triggering appear.                              */
120             if (0 !== settings.event.indexOf("scroll")) {
121                 $self.bind(settings.event, function(event) {
122                     if (!self.loaded) {
123                         $self.trigger("appear");
124                     }
125                 });
126             }
127         });
128 
129         /* Check if something appears when window is resized. */
130         $window.bind("resize", function(event) {
131             update();
132         });
133 
134         /* Force initial check if images should appear. */
135         update();
136         
137         return this;
138     };
139 
140     /* Convenience methods in jQuery namespace.           */
141     /* Use as  $.belowthefold(element, {threshold : 100, container : window}) */
142 
143     $.belowthefold = function(element, settings) {
144         var fold;
145         
146         if (settings.container === undefined || settings.container === window) {
147             fold = $window.height() + $window.scrollTop();
148         } else {
149             fold = $(settings.container).offset().top + $(settings.container).height();
150         }
151 
152         return fold <= $(element).offset().top - settings.threshold;
153     };
154     
155     $.rightoffold = function(element, settings) {
156         var fold;
157 
158         if (settings.container === undefined || settings.container === window) {
159             fold = $window.width() + $window.scrollLeft();
160         } else {
161             fold = $(settings.container).offset().left + $(settings.container).width();
162         }
163 
164         return fold <= $(element).offset().left - settings.threshold;
165     };
166         
167     $.abovethetop = function(element, settings) {
168         var fold;
169         
170         if (settings.container === undefined || settings.container === window) {
171             fold = $window.scrollTop();
172         } else {
173             fold = $(settings.container).offset().top;
174         }
175 
176         return fold >= $(element).offset().top + settings.threshold  + $(element).height();
177     };
178     
179     $.leftofbegin = function(element, settings) {
180         var fold;
181         
182         if (settings.container === undefined || settings.container === window) {
183             fold = $window.scrollLeft();
184         } else {
185             fold = $(settings.container).offset().left;
186         }
187 
188         return fold >= $(element).offset().left + settings.threshold + $(element).width();
189     };
190 
191     $.inviewport = function(element, settings) {
192          return !$.rightofscreen(element, settings) && !$.leftofscreen(element, settings) && 
193                 !$.belowthefold(element, settings) && !$.abovethetop(element, settings);
194      };
195 
196     /* Custom selectors for your convenience.   */
197     /* Use as $("img:below-the-fold").something() */
198 
199     $.extend($.expr[':'], {
200         "below-the-fold" : function(a) { return $.belowthefold(a, {threshold : 0}); },
201         "above-the-top"  : function(a) { return !$.belowthefold(a, {threshold : 0}); },
202         "right-of-screen": function(a) { return $.rightoffold(a, {threshold : 0}); },
203         "left-of-screen" : function(a) { return !$.rightoffold(a, {threshold : 0}); },
204         "in-viewport"    : function(a) { return !$.inviewport(a, {threshold : 0}); },
205         /* Maintain BC for couple of versions. */
206         "above-the-fold" : function(a) { return !$.belowthefold(a, {threshold : 0}); },
207         "right-of-fold"  : function(a) { return $.rightoffold(a, {threshold : 0}); },
208         "left-of-fold"   : function(a) { return !$.rightoffold(a, {threshold : 0}); }
209     });
210 
211 })(jQuery, window);

jQuery 2😉

 

  下面我们来看引入情势。

  jQuery 3

      
你不能够不改变你的HTML代码。将SRC天性的占位符图像。演示页面使用一×一像素的灰度GIF 的实在图像的U途胜L必须投入原始数据的属性。

  jQuery 4

  上面我们来看在html代码中须求书写些什么,那是调用格局。

  

jQuery 5😉

1 <script type="text/javascript">      
2 $(document).ready(function() { 
3          $("img").lazyload({ 
4                 effect: "fadeIn"
5           });      
6 });  
7 </script>

jQuery 6😉

 

    到那边,其实早就落到实处了大家想要的法力了,图片将会以 fadeIn
格局被载入。

  可是,我们在这里照旧不得不提一下,那一个插件的别的的功用。

一.得以设置阀值来控制 灵敏度,下面代码把阀值设置成200
注解当图片并未有观望前边先load 200像素。

  

1 $("img").lazyload({ threshold : 200 });   

 

②.足以透过安装占位符图片和自定事件来触发加载图片事件

  

1 $("img").lazyload({
2       placeholder: "img/grey.gif",      event: "click" 
3  });    

 

三.轩然大波触发加载,事件能够是别的 jQuery 时间, 如: click 和 mouseover.
你还足以应用自定义的风云, 如: sporty 和 foobar. 暗中同意情形下处于等候景况,
直到用户滚动到窗口上海体育地方片所在地方. 在浅梅红占位图片被点击在此以前阻止加载图片,
你能够如此做:

1 $("img").lazyload({
2       placeholder: "img/grey.gif",      event: "click" 
3  });    

 

推迟加载图片,Lazy Load 插件的一个不完整的效能,
不过那也能用来兑现图片的推移加载. 上面包车型大巴代码实现了页面加载成功后再加载.
页面加载成功 伍 秒后, 钦命区域内的图样会活动进行加载.

jQuery 7😉

 1 $(function() {
 2       $("img:below-the-fold").lazyload({                                    
 3           placeholder: "img/grey.gif",
 4           event: "sporty"
 5 });  
 6 });  
 7 $(window).bind("load", function() {
 8       var timeout = setTimeout(function() {
 9       $("img").trigger("sporty") }, 5000);  
10 }); 

jQuery 8jQuery,;)

越来越多的艺术和材料,请参见官网http://www.appelsiini.net/projects/lazyload

此处需求特别说美赞臣下,jquery的版本不要太低哦,一.叁的就不会出职能的。

那是小编写的三个小demo,拿去参考一下或然极好的。

下载地址:http://files.cnblogs.com/wscdzl/demo.rar

  

相关文章

网站地图xml地图