即使按正确的顺序,函数也未定义?

时间:2018-09-15 19:02:54

标签: javascript jquery performance lazy-loading

我正在使用与jQuery配合使用的lazyload函数。

我在我的 liga.js 文件中这样称呼它:

lazyload();

现在,在该页面的页脚处:

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="code.jquery.com/ui/1.12.1/jquery-ui.min.js" ></script>
<script src="/includes/jquery.lazyload.min.js"></script>
<script src="/includes/liga.js"></script>

如您所见,我的liga.js文件位于列表的最后。

我也尝试过这样调用函数:

$(document).ready(function(){
lazyload();
...

我不断收到错误消息:

  

ReferenceError:未定义延迟加载

那是为什么?

1 个答案:

答案 0 :(得分:1)

jQuery包装器的语法为$(selector).lazy() ...

$(function() {

    $('.iam').lazy({

      /* called before an element gets handled */
      beforeLoad: function(element) {
        var imageSrc = element.data('src');
        console.log('image "' + imageSrc + '" is about to be loaded');
      },
           
      /* called after an element was successfully handled */
      afterLoad: function(element) {
        var imageSrc = element.data('src');
        console.log('image "' + imageSrc + '" was loaded successfully');
      },
            
      /* called whenever an element could not be handled */
      onError: function(element) {
        var imageSrc = element.data('src');
        console.log('image "' + imageSrc + '" could not be loaded');
      },
            
      /* called once all elements were handled */
      onFinishedAll: function() {
        console.log('finished loading all images');
      }
    });
});
<script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.lazy/1.7.10/jquery.lazy.min.js"></script>

<img class="iam" data-src="http://www.ektoplazm.com/img/escape-into-the-mirror.jpg" width="640" height="640" />