在 Blogger 中同时使用 Lazy Load 和 Auto Image Alt and Title Changer 脚本

Using both Lazy Load and Auto Image Alt and Title Changer scripts at the same time in Blogger

我有一个博客http://www.rawdevart.com/
我已经安装了一个脚本,用于自动更改加载的 post 中所有图像的 alttitle。 我使用的脚本如下:

<b:if cond='data:blog.homepageUrl!=data:blog.url'>
 <script type='text/javascript'>  


 //<![CDATA[  
 $(document).ready(function() {  
  $('img').each(function(){  
   var $img = $(this);  
   var filename = $img.attr('src')  
   var returnt=filename.substring((filename.lastIndexOf('/'))+1, filename.lastIndexOf('.'));

      var ctitle=returnt.replace(/%2B|_/g, " ").replace(/%2521|_/g,"!");

if((returnt=="if_go-previous_118774")||(returnt=="if_go-next_118773"))
   {
    if(returnt=="if_go-previous_118774")
      {
        $img.attr('title', "Previous Chapter");
        $img.attr('alt' ,"Previous Chapter");
      }
   else if(returnt=="if_go-next_118773")
      {
        $img.attr('title', "Next Chapter");
        $img.attr('alt' ,"Next Chapter");
      }
   else if(returnt=="ch-list")
      {
        $img.attr('title', "Chapter List");
        $img.attr('alt' ,"Chapter List");
      }
   }else{
   ctitle=getTitle+" Page "+ctitle;
   $img.attr('title', ctitle);
   $img.attr('alt' ,ctitle);
   }  
  });  
 }); 

 //]]>  

我有一些照片想用特定的值重命名,所以我在里面添加了很多 if。

这是我在互联网上找到的 LazyLoad Images Script,据说可以在 Blogger 中使用。

<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>

在上面的延迟加载脚本中,有一个特定的部分给我带来了问题: placeholder:"http://i22.servimg.com/u/f22/15/42/72/40/grey10.gif"

这部分脚本的使用是使所有图像 alttitle 值变得像 *Title of the chapter* grey10.

顺便说一句,变量getTitle用于获取章节名称。

这是我面临的问题的一个例子:

假设在这个 link 中: http://www.rawdevart.com/2017/09/legend-chapter-6-raw-manga.html

当您将鼠标移到一张图片上时,您可以看到第二张图片的 altLegend Chapter 6 Raw Manga Page 002

002 是图像 link 的最后一个字符 URL/002.jpeg,它正在通过脚本进行处理,并且仅制作为 002,然后使用 getTitle 添加。

但是在使用延迟加载后,alt 变成了 Legend Chapter 6 Raw Manga Page grey10

正如我所见,逻辑是如何工作的,当页面加载时,第一张图片原封不动地加载,第二张图片被占位符 gif 文件替换。然后当我们向下移动时,gif 图像将被替换为原始图像。但是这样做,并没有获得我想要的alt和title。

那么有没有一种方法可以同时使用这两个脚本而不丢失任何功能?请帮忙

顺便说一句,您可以在 link http://i22.servimg.com/u/f22/15/42/72/40/grey10.gif 中放入任何内容,将 grey10 替换为 http://i22.servimg.com/u/f22/15/42/72/40/anyshit.gif 之类的任何字母,它仍然会提供 1x1 分辨率的图像。希望这会有所帮助。

如果没有看到您的 HTML,很难确切地知道发生了什么,但是如果该图像导致问题,只需在您的处理中跳过它:

$(document).ready(function() {  
    $('img').each(function(){  
        var $img = $(this);  
        var filename = $img.attr('src')  
        var returnt=filename.substring((filename.lastIndexOf('/'))+1, filename.lastIndexOf('.'));
        var ctitle=returnt.replace(/%2B|_/g, " ").replace(/%2521|_/g,"!");

        // If this is the file causing problems, skip over it and continue
        // with the next image.
        if (filename === 'http://i22.servimg.com/u/f22/15/42/72/40/grey10.gif') {
            continue;
        }

        // ... etc, rest of your code

如果导致问题的延迟加载脚本中有多个占位符图像,并且它们托管在同一台服务器上,并且 none 个您想要的图像 处理在那个服务器上,也许基于 URL 跳过会更有意义:

if (filename.indexOf('servimg.com') >= 0) {
    continue;
}

因为问题是使用延迟加载脚本时选择了错误的图像 URL。我们可以检查图像元素中是否存在 original 属性(它始终包含正确的图像 URL),并使用它来分配 titlealt 属性。在脚本中更改行 -

var filename = $img.attr('src');

至-

if($img.attr('original')){
    var filename = $img.attr('original');
} else {
    var filename = $img.attr('src');
}