jquery.lazyload.js 图片延迟加载
jQuery页面图片伴随滚动条逐渐显示
根据实际应用可以进行相应的修改
// 天涯版 (function(jQuery) { jQuery.fn.lazyload = function(options) { var settings = { threshold : 0, failurelimit : 0, event : "scroll", effect : "show", container : window }; if(options) { jQuery.extend(settings, options); } /* Fire one scroll event per scroll. Not one scroll event per image. */ var elements = this; if ("scroll" == settings.event) { jQuery(settings.container).bind("scroll", function(event) { var counter = 0; elements.each(function() { if (jQuery.abovethetop(this, settings)) { /* Nothing. */ } else if (!jQuery.belowthefold(this, settings)) { jQuery(this).trigger("appear"); } else { if (counter++ > settings.failurelimit) { return false; } } }); /* Remove image from array so it is not looped next time. */ var temp = jQuery.grep(elements, function(element) { return !element.loaded; }); elements = jQuery(temp); }); } this.each(function() { var self = this; /* Save original only if it is not defined in HTML. */ if (undefined != jQuery(self).attr("original")){ self.loaded = false; /* When appear is triggered load original image. */ jQuery(self).one("appear", function() { if (!this.loaded) { jQuery("<img />") .bind("load", function() { jQuery(self) .hide() .removeAttr("height") .attr("src", jQuery(self).attr("original")) [settings.effect](settings.effectspeed); self.loaded = true; }) .attr("src", jQuery(self).attr("original")); }; }); } }); /* Force initial check if images should appear. */ jQuery(settings.container).trigger(settings.event); return this; }; /* Convenience methods in jQuery namespace. */ /* Use as jQuery.belowthefold(element, {threshold : 100, container : window}) */ function checkshow(){ } jQuery.belowthefold = function(element, settings) { if (settings.container === undefined || settings.container === window) { var fold = jQuery(window).height()*2 + jQuery(window).scrollTop(); } else { var fold = jQuery(settings.container).offset().top + jQuery(settings.container).height()*2; } return fold <= jQuery(element).offset().top - settings.threshold; }; jQuery.abovethetop = function(element, settings) { if (settings.container === undefined || settings.container === window) { var fold = jQuery(window).scrollTop(); } else { var fold = jQuery(settings.container).offset().top; } return fold >= jQuery(element).offset().top + settings.threshold + jQuery(element).height(); }; /* Custom selectors for your convenience. */ /* Use as jQuery("img:below-the-fold").something() */ jQuery.extend(jQuery.expr[':'], { "below-the-fold" : "jQuery.belowthefold(a, {threshold : 0, container: window})", "above-the-fold" : "!jQuery.belowthefold(a, {threshold : 0, container: window})" }); })(jQuery); jQuery("#pContentDiv .post img[original]").lazyload({ effect:"fadeIn" });
本文由用户 openkk 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!