• Lazy Load 源码学习注释笔记



    /*
    ! * Lazy Load - jQuery plugin for lazy loading images * * Copyright (c) 2007-2015 Mika Tuupola * * Licensed under the MIT license: * http://www.opensource.org/licenses/mit-license.php * * Project home: * http://www.appelsiini.net/projects/lazyload * * Version: 1.9.7 * */ (function($, window, document, undefined) { var $window = $(window); $.fn.lazyload = function(options) { var elements = this; //this指向谁?指向函数的所有者。例如 $("img.lazy").lazyload();,this代表的是$("img.lazy")选择器中的元素数组 var $container; var settings = { threshold : 0, failure_limit : 0, event : "scroll",//初始触发事件时scroll effect : "show", container : window, data_attribute : "original", skip_invisible : false, appear : null, load : null, placeholder : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" }; function update() { var counter = 0; elements.each(function() { var $this = $(this);//this指的是每个img元素 if (settings.skip_invisible && !$this.is(":visible")) { return; } if ($.abovethetop(this, settings) || $.leftofbegin(this, settings)) {//如果指定的容器有一部分超出了屏幕最上端或最左端 /* Nothing. */ } else if (!$.belowthefold(this, settings) && !$.rightoffold(this, settings)) { $this.trigger("appear"); /*以上是加载条件,即:图片已经暴露,或者到了配置的暴露极限值*/ /* if we found an image we'll load, reset the counter */ counter = 0; } else { if (++counter > settings.failure_limit) { return false; } } }); } if(options) { /* Maintain BC for a couple of versions. */ if (undefined !== options.failurelimit) { options.failure_limit = options.failurelimit; delete options.failurelimit; } if (undefined !== options.effectspeed) { options.effect_speed = options.effectspeed; delete options.effectspeed; } $.extend(settings, options); } /* Cache container as jQuery as object. */ $container = (settings.container === undefined || settings.container === window) ? $window : $(settings.container); /* Fire one scroll event per scroll. Not one scroll event per image. */ if (0 === settings.event.indexOf("scroll")) { $container.on(settings.event, function() {//1.只要有对应事件触发,就会调用update return update(); }); } this.each(function() {//对选择器中的元素进行遍历处理 var self = this;//到了这里,*this*就代表每一个元素了,为了避免混淆,把this赋值给self var $self = $(self); self.loaded = false; /* If no src attribute given use data:uri. */ if ($self.attr("src") === undefined || $self.attr("src") === false) { if ($self.is("img")) { $self.attr("src", settings.placeholder); } } /* When appear is triggered load original image. */ $self.one("appear", function() {//一旦出现在浏览器的view port中(注意这里用了one,意思是,只在第一次触发此事件时执行以下的动作) if (!this.loaded) {//这个图片尚未定义为:已加载 if (settings.appear) {//跳过,初始为null var elements_left = elements.length; settings.appear.call(self, elements_left, settings); } //动态创建DOM元素 /* $("<img />") .one("load", function() {*///动态创建的img元素加载完成时的动作 var original = $self.attr("data-" + settings.data_attribute); //alert(111) $self.hide();//设置为display:none,意为阻止加载 if ($self.is("img")) { $self.attr("src", original); } else { $self.css("background-image", "url('" + original + "')"); } $self[settings.effect](settings.effect_speed); // $self['show'](argument),等同于 $self.show(argument),此时图片开始加载 self.loaded = true;//标记为已经加载 /* Remove image from array so it is not looped next time. */ //为了优化,下次循环时不要带上 var temp = $.grep(elements, function(element) { return !element.loaded; }); elements = $(temp);//轻装上阵? if (settings.load) {//初始为null,跳过 var elements_left = elements.length; settings.load.call(self, elements_left, settings); } /*}) .attr("src", $self.attr("data-" + settings.data_attribute));*///赋给src属性,去加载吧!我的图片! } }); /* When wanted event is triggered load original image */ /* by triggering appear. */ if (0 !== settings.event.indexOf("scroll")) {//如果settings.event中,scroll并不是在首部出现 $self.on(settings.event, function() { if (!self.loaded) { $self.trigger("appear"); } }); } }); /* Check if something appears when window is resized. */ $window.on("resize", function() { update(); }); /* With IOS5 force loading images when navigating with back button. */ /* Non optimal workaround. */ if ((/(?:iphone|ipod|ipad).*os 5/gi).test(navigator.appVersion)) { $window.on("pageshow", function(event) { if (event.originalEvent && event.originalEvent.persisted) { elements.each(function() { $(this).trigger("appear"); }); } }); } /* Force initial check if images should appear. */ $(document).ready(function() { update(); }); return this; }; /* Convenience methods in jQuery namespace. */ /* Use as $.belowthefold(element, {threshold : 100, container : window}) */ //以下会判断,是否满足了加载条件,满足会返回false $.belowthefold = function(element, settings) { var fold; if (settings.container === undefined || settings.container === window) { fold = (window.innerHeight ? window.innerHeight : $window.height()) + $window.scrollTop();//文档超出屏幕的高度+window本身的高度 //console.log(fold) } else { fold = $(settings.container).offset().top + $(settings.container).height(); } /* offset() 方法返回或设置匹配元素相对于文档的偏移(位置)。 如果文档在屏幕上方的高度+屏幕本身的高度,大于元素在文档中的纵向偏移量(从文档顶端到达元素的纵向距离),那就说明元素已经暴露出来了,则返回true 如果设定了threshold,则表示:元素还没出现时,差多少px就开始加载 */ return fold <= $(element).offset().top - settings.threshold; }; $.rightoffold = function(element, settings) { var fold; if (settings.container === undefined || settings.container === window) { fold = $window.width() + $window.scrollLeft(); } else { fold = $(settings.container).offset().left + $(settings.container).width(); } return fold <= $(element).offset().left - settings.threshold; }; $.abovethetop = function(element, settings) { var fold; if (settings.container === undefined || settings.container === window) { fold = $window.scrollTop(); } else { fold = $(settings.container).offset().top; } return fold >= $(element).offset().top + settings.threshold + $(element).height(); }; $.leftofbegin = function(element, settings) { var fold; if (settings.container === undefined || settings.container === window) { fold = $window.scrollLeft(); } else { fold = $(settings.container).offset().left; } return fold >= $(element).offset().left + settings.threshold + $(element).width(); }; $.inviewport = function(element, settings) { return !$.rightoffold(element, settings) && !$.leftofbegin(element, settings) && !$.belowthefold(element, settings) && !$.abovethetop(element, settings); }; /* Custom selectors for your convenience. */ /* Use as $("img:below-the-fold").something() or */ /* $("img").filter(":below-the-fold").something() which is faster */ $.extend($.expr[":"], { "below-the-fold" : function(a) { return $.belowthefold(a, {threshold : 0}); }, "above-the-top" : function(a) { return !$.belowthefold(a, {threshold : 0}); }, "right-of-screen": function(a) { return $.rightoffold(a, {threshold : 0}); }, "left-of-screen" : function(a) { return !$.rightoffold(a, {threshold : 0}); }, "in-viewport" : function(a) { return $.inviewport(a, {threshold : 0}); }, /* Maintain BC for couple of versions. */ "above-the-fold" : function(a) { return !$.belowthefold(a, {threshold : 0}); }, "right-of-fold" : function(a) { return $.rightoffold(a, {threshold : 0}); }, "left-of-fold" : function(a) { return !$.rightoffold(a, {threshold : 0}); } }); })(jQuery, window, document);
  • 相关阅读:
    跟我学SpringCloud | 第十二篇:Spring Cloud Gateway初探
    跟我学SpringCloud | 第十一篇:使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪
    跟我学SpringCloud | 第十篇:服务网关Zuul高级篇
    跟我学SpringCloud | 第九篇:服务网关Zuul初
    跟我学SpringCloud | 第八篇:Spring Cloud Bus 消息总线
    跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh
    跟我学SpringCloud | 第六篇:Spring Cloud Config Github配置中心
    跟我学SpringCloud | 第五篇:熔断监控Hystrix Dashboard和Turbine
    跟我学SpringCloud | 第四篇:熔断器Hystrix
    跟我学SpringCloud | 第三篇:服务的提供与Feign调用
  • 原文地址:https://www.cnblogs.com/ch459742906/p/7207209.html
Copyright © 2020-2023  润新知