Blogger上的延迟加载

时间:2012-05-06 05:20:08

标签: javascript jquery html blogger

我正在尝试在博客博客上实施延迟加载,但我遇到了一些问题。有问题的延迟加载插件就是这个:http://www.appelsiini.net/projects/lazyload

所以在文档中,它表示延迟加载的图像应该是以下html代码格式:

<img class="lazy" src="img/grey.gif" data-original="img/example.jpg"  width="640" heigh="480">

与博客帖子一样,博客引擎按以下格式输出图像:

<img class="lazy" src="img/myimage.png" width="640" heigh="480">

我的问题是,是否可以让博主输出第一种格式的图像html,“grey.gif”spaceholder作为所有图像的默认图像src,以及具有正确图像的“data-original”属性URL,以便lazyloader实际工作?

提前感谢任何帮助: - )

3 个答案:

答案 0 :(得分:1)

您可以切换到HTML视图,然后根据延迟加载脚本的要求修改每个img标记。

答案 1 :(得分:-1)

您不需要执行所有这些操作,您可以使用专为博客博客创建的以下延迟加载脚本

<script type='text/javascript'>//<![CDATA[ (function(a){a.fn.lazyload=function(b){var c={threshold:0,failurelimit:0,event:"scroll",effect:"show",container:window};if(b){a.extend(c,b)}var d=this;if("scroll"==c.event){a(c.container).bind("scroll",function(b){var e=0;d.each(function(){if(a.abovethetop(this,c)||a.leftofbegin(this,c)){}else if(!a.belowthefold(this,c)&&!a.rightoffold(this,c)){a(this).trigger("appear")}else{if(e++>c.failurelimit){return false}}});var f=a.grep(d,function(a){return!a.loaded});d=a(f)})}this.each(function(){var b=this;if(undefined==a(b).attr("original")){a(b).attr("original",a(b).attr("src"))}if("scroll"!=c.event||undefined==a(b).attr("src")||c.placeholder==a(b).attr("src")||a.abovethetop(b,c)||a.leftofbegin(b,c)||a.belowthefold(b,c)||a.rightoffold(b,c)){if(c.placeholder){a(b).attr("src",c.placeholder)}else{a(b).removeAttr("src")}b.loaded=false}else{b.loaded=true}a(b).one("appear",function(){if(!this.loaded){a("<img />").bind("load",function(){a(b).hide().attr("src",a(b).attr("original"))[c.effect](c.effectspeed);b.loaded=true}).attr("src",a(b).attr("original"))}});if("scroll"!=c.event){a(b).bind(c.event,function(c){if(!b.loaded){a(b).trigger("appear")}})}});a(c.container).trigger(c.event);return this};a.belowthefold=function(b,c){if(c.container===undefined||c.container===window){var d=a(window).height()+a(window).scrollTop()}else{var d=a(c.container).offset().top+a(c.container).height()}return d<=a(b).offset().top-c.threshold};a.rightoffold=function(b,c){if(c.container===undefined||c.container===window){var d=a(window).width()+a(window).scrollLeft()}else{var d=a(c.container).offset().left+a(c.container).width()}return d<=a(b).offset().left-c.threshold};a.abovethetop=function(b,c){if(c.container===undefined||c.container===window){var d=a(window).scrollTop()}else{var d=a(c.container).offset().top}return d>=a(b).offset().top+c.threshold+a(b).height()};a.leftofbegin=function(b,c){if(c.container===undefined||c.container===window){var d=a(window).scrollLeft()}else{var d=a(c.container).offset().left}return d>=a(b).offset().left+c.threshold+a(b).width()};a.extend(a.expr[":"],{"below-the-fold":"$.belowthefold(a, {threshold : 0, container: window})","above-the-fold":"!$.belowthefold(a, {threshold : 0, container: window})","right-of-fold":"$.rightoffold(a, {threshold : 0, container: window})","left-of-fold":"!$.rightoffold(a, {threshold : 0, container: window})"})})(jQuery);$(function(){$("img").lazyload({placeholder:"http://i22.servimg.com/u/f22/15/42/72/40/grey10.gif",effect:"fadeIn",threshold:"-50"})})//]]></script>

您可以将http://i22.servimg.com/u/f22/15/42/72/40/grey10.gif替换为其他图片链接作为占位符。

来源链接: http://101helper.blogspot.com/2014/08/how-to-speed-up-your-blogger-blog.html

答案 2 :(得分:-1)

您可以在 js 函数中使用 loading='lazy'