2

我正在使用 jquery.lazyload 来加载我的图像。根据样本,一切正常。

现在,当原始图像不是加载器或错误时,我需要将占位符图像更改为我的“no-image.jpg”。

我得到了与此相关的以下线程。 无法让 LazyLoad 和 onerror 在图像上一起工作

根据线程,我更新了延迟加载 js 文件并包含错误图像的脚本。请检查我的html如下;

 <!DOCTYPE html>
<html>
<head>
    <title>Lazy Load Enabled</title>
    <style>
        .container img {
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <img class="lazy" data-original="img/1.jpg" width="765" height="574" /><br />
        <img class="lazy" data-original="img/2.jpg" width="765" height="574" /><br />
        <img class="lazy" data-original="img/error.jpg" width="765" height="574"><br />
        <img class="lazy" data-original="img/4.jpg" width="765" height="574" /><br />
        <img class="lazy" data-original="img/5.jpg" width="765" height="574" /><br />
        <img class="lazy" data-original="img/6.jpg" width="765" height="574" /><br />
        <img class="lazy" data-original="img/7.jpg" width="765" height="574" /><br />
        <img class="lazy" data-original="img/8.jpg" width="765" height="574" /><br />
    </div>

    <script type="text/javascript" src="script/jquery-1.8.3.min.js"></script>
    <script type="text/javascript" src="script/jquery.lazyload.js"></script>
    <script type="text/javascript" charset="utf-8">
        $(function () {
            $(".lazy").error(function () {
                $(this).attr('src', 'img/no-image.jpg');
            });

            $("img.lazy").lazyload();
        });
    </script>
</body>
</html>

这里我故意放了文件夹中不存在的error.jpg。我的lazyload js是;

    $.fn.lazyload = function(options) {
    var elements = this;
    var $container;
    var settings = {
        threshold       : 0,
        failure_limit   : 0,
        event           : "scroll",
        effect          : "show",
        container       : window,
        data_attribute  : "original",
        skip_invisible  : false,
        appear          : function(ele,settings)
        {
            $(ele).attr("src",$(ele).attr("data-original"));
        },
        load: null,
        placeholder     : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC"
    };

这里我只给出了js文件的一部分。

但是当页面看到时,显示相同的占位符图像(空)而不是我的'no-image.jpg'

4

1 回答 1

0

you can write like this;

  $("img.lazy").lazyload({
            placeholder     : "img/no-image.jpg"
        });

or you can use convert to base64 image with this site and write like this;

   $("img.lazy").lazyload({
        placeholder     : "data:image/gif;base64,R0lGODlhkAGQAcQAAAAAAP////v7+/f39/b29vPz8/Ly8u7u7u3t7erq6unp6ebm5uXl5eLi4uDg4N7e3tzc3Nra2tfX19bW1tPT09HR0c/Pz83NzcrKysnJycbGxsXFxcHBwb29vf///wAAACH5BAEAAB4ALAAAAACQAZABAAX/YCCOZGmeaKqubOu+cCzPdG3feK7vfO//wKBwSCwaj8ikcslsOp/QqHRKrVqv2Kx2y+16v+CweEwum8/otHrNbrvf8Lh8Tq/b7/i8fs/v+/+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/wADChxIsKDBgwgTKlzIsKHDhxAjSpxIsaLFixgzatzIsaPHjyBDihxJsqTJkyhT/6pcybKly5cwY8qcSbOmzZs4c+rcybOnzxEHDjR4QLSoUaJBf+oRWuFCh6dQo0qdeiHCggIGj2rdWhSrkgVcwyKdIlTD1LNo03Ko0GCAQLVwLyxxCvfsASgJJnCoy7duhrb/+qJdoISu4Kd3mzzYcLixWrb9HEsVkMTw4cSFJWtWS1jf5qcTKkvGfCTC59OD86EmTcSyYNZDFuxFTVvqBa/1UGdA4rov7CAYagufGoEyPdqAi/Tm+9tHgtnDo0MlcJy2W+WjiSyWzh1qcni1KxhZXrf5Dr3d03cIHU+4eR7k4b7HkUG9/d3ghRsXEp8zcPsA4ufOcA+0lt0P9QEY4P9+60R33X+OzUdDggou2E50cvF3IHwVdiigO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