博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jq图片懒加载
阅读量:5122 次
发布时间:2019-06-13

本文共 10624 字,大约阅读时间需要 35 分钟。

jQuery图片延迟加载插件jQuery.lazyload,使用延迟加载在可提高网页下载速度。在某些情况下,它也能帮助减轻服务器负载。

使用方法

引用jquery和jquery.lazyload.js到你的页面

html图片调用方法

为图片加入样式lazy  图片路径引用方法用data-original

js出始化lazyload并设置图片显示方式

在图片中也可以不使用 class="lazy",初始化时使用:$("img").lazyload({effect: "fadeIn"});

这样就可以对全局的图片都有效!

 参数设置

$("img.lazy").lazyload({  placeholder : "img/grey.gif", //用图片提前占位    // placeholder,值为某一图片路径.此图片用来占据将要加载的图片的位置,待图片加载时,占位图则会隐藏  effect: "fadeIn", // 载入使用何种效果    // effect(特效),值有show(直接显示),fadeIn(淡入),slideDown(下拉)等,常用fadeIn  threshold: 200, // 提前开始加载    // threshold,值为数字,代表页面高度.如设置为200,表示滚动条在离目标位置还有200的高度时就开始加载图片,可以做到不让用户察觉  event: 'click',  // 事件触发时才加载    // event,值有click(点击),mouseover(鼠标划过),sporty(运动的),foobar(…).可以实现鼠标莫过或点击图片才开始加载,后两个值未测试…  container: $("#container"),  // 对某容器中的图片实现效果    // container,值为某容器.lazyload默认在拉动浏览器滚动条时生效,这个参数可以让你在拉动某DIV的滚动条时依次加载其中的图片  failurelimit : 10 // 图片排序混乱时     // failurelimit,值为数字.lazyload默认在找到第一张不在可见区域里的图片时则不再继续加载,但当HTML容器混乱的时候可能出现可见区域内图片并没加载出来的情况,failurelimit意在加载N张可见区域外的图片,以避免出现这个问题.});

 懒加载jquery.lazyload.js  js代码为下边

1 /*  2  * Lazy Load - jQuery plugin for lazy loading images  3  *  4  * Version:  1.9.3  5  *  6  */  7   8 (function($, window, document, undefined) {  9     var $window = $(window); 10  11     $.fn.lazyload = function(options) { 12         var elements = this; 13         var $container; 14         var settings = { 15             threshold       : 0, 16             failure_limit   : 0, 17             event           : "scroll", 18             effect          : "show", 19             container       : window, 20             data_attribute  : "original", 21             skip_invisible  : true, 22             appear          : null, 23             load            : null, 24             placeholder     : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" 25         }; 26  27         function update() { 28             var counter = 0; 29  30             elements.each(function() { 31                 var $this = $(this); 32                 if (settings.skip_invisible && !$this.is(":visible")) { 33                     return; 34                 } 35                 if ($.abovethetop(this, settings) || 36                     $.leftofbegin(this, settings)) { 37                         /* Nothing. */ 38                 } else if (!$.belowthefold(this, settings) && 39                     !$.rightoffold(this, settings)) { 40                         $this.trigger("appear"); 41                         /* if we found an image we'll load, reset the counter */ 42                         counter = 0; 43                 } else { 44                     if (++counter > settings.failure_limit) { 45                         return false; 46                     } 47                 } 48             }); 49  50         } 51  52         if(options) { 53             /* Maintain BC for a couple of versions. */ 54             if (undefined !== options.failurelimit) { 55                 options.failure_limit = options.failurelimit; 56                 delete options.failurelimit; 57             } 58             if (undefined !== options.effectspeed) { 59                 options.effect_speed = options.effectspeed; 60                 delete options.effectspeed; 61             } 62  63             $.extend(settings, options); 64         } 65  66         /* Cache container as jQuery as object. */ 67         $container = (settings.container === undefined || 68                       settings.container === window) ? $window : $(settings.container); 69  70         /* Fire one scroll event per scroll. Not one scroll event per image. */ 71         if (0 === settings.event.indexOf("scroll")) { 72             $container.bind(settings.event, function() { 73                 return update(); 74             }); 75         } 76  77         this.each(function() { 78             var self = this; 79             var $self = $(self); 80  81             self.loaded = false; 82  83             /* If no src attribute given use data:uri. */ 84             if ($self.attr("src") === undefined || $self.attr("src") === false) { 85                 if ($self.is("img")) { 86                     $self.attr("src", settings.placeholder); 87                 } 88             } 89  90             /* When appear is triggered load original image. */ 91             $self.one("appear", function() { 92                 if (!this.loaded) { 93                     if (settings.appear) { 94                         var elements_left = elements.length; 95                         settings.appear.call(self, elements_left, settings); 96                     } 97                     $("") 98                         .bind("load", function() { 99 100                             var original = $self.attr("data-" + settings.data_attribute);101                             $self.hide();102                             if ($self.is("img")) {103                                 $self.attr("src", original);104                             } else {105                                 $self.css("background-image", "url('" + original + "')");106                             }107                             $self[settings.effect](settings.effect_speed);108 109                             self.loaded = true;110 111                             /* Remove image from array so it is not looped next time. */112                             var temp = $.grep(elements, function(element) {113                                 return !element.loaded;114                             });115                             elements = $(temp);116 117                             if (settings.load) {118                                 var elements_left = elements.length;119                                 settings.load.call(self, elements_left, settings);120                             }121                         })122                         .attr("src", $self.attr("data-" + settings.data_attribute));123                 }124             });125 126             /* When wanted event is triggered load original image */127             /* by triggering appear.                              */128             if (0 !== settings.event.indexOf("scroll")) {129                 $self.bind(settings.event, function() {130                     if (!self.loaded) {131                         $self.trigger("appear");132                     }133                 });134             }135         });136 137         /* Check if something appears when window is resized. */138         $window.bind("resize", function() {139             update();140         });141 142         /* With IOS5 force loading images when navigating with back button. */143         /* Non optimal workaround. */144         if ((/(?:iphone|ipod|ipad).*os 5/gi).test(navigator.appVersion)) {145             $window.bind("pageshow", function(event) {146                 if (event.originalEvent && event.originalEvent.persisted) {147                     elements.each(function() {148                         $(this).trigger("appear");149                     });150                 }151             });152         }153 154         /* Force initial check if images should appear. */155         $(document).ready(function() {156             update();157         });158 159         return this;160     };161 162     /* Convenience methods in jQuery namespace.           */163     /* Use as  $.belowthefold(element, {threshold : 100, container : window}) */164 165     $.belowthefold = function(element, settings) {166         var fold;167 168         if (settings.container === undefined || settings.container === window) {169             fold = (window.innerHeight ? window.innerHeight : $window.height()) + $window.scrollTop();170         } else {171             fold = $(settings.container).offset().top + $(settings.container).height();172         }173 174         return fold <= $(element).offset().top - settings.threshold;175     };176 177     $.rightoffold = function(element, settings) {178         var fold;179 180         if (settings.container === undefined || settings.container === window) {181             fold = $window.width() + $window.scrollLeft();182         } else {183             fold = $(settings.container).offset().left + $(settings.container).width();184         }185 186         return fold <= $(element).offset().left - settings.threshold;187     };188 189     $.abovethetop = function(element, settings) {190         var fold;191 192         if (settings.container === undefined || settings.container === window) {193             fold = $window.scrollTop();194         } else {195             fold = $(settings.container).offset().top;196         }197 198         return fold >= $(element).offset().top + settings.threshold  + $(element).height();199     };200 201     $.leftofbegin = function(element, settings) {202         var fold;203 204         if (settings.container === undefined || settings.container === window) {205             fold = $window.scrollLeft();206         } else {207             fold = $(settings.container).offset().left;208         }209 210         return fold >= $(element).offset().left + settings.threshold + $(element).width();211     };212 213     $.inviewport = function(element, settings) {214          return !$.rightoffold(element, settings) && !$.leftofbegin(element, settings) &&215                 !$.belowthefold(element, settings) && !$.abovethetop(element, settings);216      };217 218     /* Custom selectors for your convenience.   */219     /* Use as $("img:below-the-fold").something() or */220     /* $("img").filter(":below-the-fold").something() which is faster */221 222     $.extend($.expr[":"], {223         "below-the-fold" : function(a) { return $.belowthefold(a, {threshold : 0}); },224         "above-the-top"  : function(a) { return !$.belowthefold(a, {threshold : 0}); },225         "right-of-screen": function(a) { return $.rightoffold(a, {threshold : 0}); },226         "left-of-screen" : function(a) { return !$.rightoffold(a, {threshold : 0}); },227         "in-viewport"    : function(a) { return $.inviewport(a, {threshold : 0}); },228         /* Maintain BC for couple of versions. */229         "above-the-fold" : function(a) { return !$.belowthefold(a, {threshold : 0}); },230         "right-of-fold"  : function(a) { return $.rightoffold(a, {threshold : 0}); },231         "left-of-fold"   : function(a) { return !$.rightoffold(a, {threshold : 0}); }232     });233 234 })(jQuery, window, document);

 

转载于:https://www.cnblogs.com/haonanZhang/p/6505688.html

你可能感兴趣的文章
线程和多线程学习
查看>>
梦断代码阅读笔记01
查看>>
Deprecated: Assigning the return value of new by reference is deprecated in报错
查看>>
第四次作业-坦克大战
查看>>
O365(世纪互联)SharePoint 之使用列表库发布新闻
查看>>
bootstrap-select详细语句分析,如有错误请指出,感谢。
查看>>
遍历三维数组
查看>>
Python语言的有限状态机实现样例
查看>>
桐桐的贸易--WA
查看>>
LightTools.v5.1_完整好用版本\
查看>>
javascript中的变量提升
查看>>
获取AWR报告
查看>>
小程序组件
查看>>
grunt学习2
查看>>
IDEA中web项目maven项目手动打war包的方式
查看>>
Spring Boot 初体验(6)Spring Boot JdbcTemplate
查看>>
Wpf Page间跳转传参数 And Window To Page
查看>>
Hammer.js
查看>>
Node.js Error: Cannot find module express的解决办法
查看>>
并发 and 并行
查看>>