【问题标题】:jQuery, JavaScript, HTML: how to load images after everything else is loaded?jQuery,JavaScript,HTML:加载其他所有内容后如何加载图像?
【发布时间】:2011-07-21 03:14:36
【问题描述】:

我有一个非常复杂的页面,其中包含很多脚本和相当长的加载时间。在该页面的顶部,我想实现 jquery Nivo Slider (http://nivo.dev7studios.com/)。

在文档中它说我必须列出 div#slider 内滑块的所有图像

<div id="slider">
    <img src="images/slide1.jpg" alt="" />
    <a href="http://dev7studios.com"><img src="images/slide2.jpg" alt="" title="#htmlcaption" /></a>
    <img src="images/slide3.jpg" alt="" title="This is an example of a caption" />
    <img src="images/slide4.jpg" alt="" />
</div>

但是我可能有 10 张 1000x400 像素的图像,这非常大。这些图像将在页面加载时加载。由于它们在我的标题中,这可能需要很长时间。

我正在寻找一种方法来使用任何 jquery 滑块插件(如 nivo 滑块),但要么动态加载图像,要么在我页面上的所有其他内容加载后加载所有这些图像。

知道如何解决这个问题吗?

有没有办法在页面上的所有其他内容加载后启动 javascript 进程?如果有办法解决我的问题(使用 jquery ajax load() 方法)...但是我不知道如何等待其他所有内容加载,然后使用所有图像启动滑块。

【问题讨论】:

  • 为什么不尝试最小化您的图像,然后将它们链接到更大的版本?

标签: javascript jquery image loading


【解决方案1】:

jquery 具有在文档加载后执行 javascript 的语法:

<script type="text/javascript">
jQuery(function(){

//your function implementation here...

});
</script>

【讨论】:

  • 这是不正确的,因为此函数绑定到 DOM 就绪事件而不是窗口加载。窗口加载事件是告诉您其他所有内容都已加载的事件。
【解决方案2】:

check outjquery load() 事件,它等待包括图形在内的所有内容

$(window).load(function () {
  // run code
});

在加载时,您可以使用以下方法加载图像:

var image = new Image();
image.src = "/path/to/huge/file.jpg";

你也可以给图片添加一个onload函数

image.onload = function() {
  ...
}

【讨论】:

    【解决方案3】:

    除了 Xhalent 的回答,使用 jQuery 中的 .append() 函数将它们添加到 DOM:

    您的 HTML 将只有:

    <div id="slider">
    </div>
    

    然后你的 jquery 将是:

    jQuery(function(){
        $("#slider").append('<img src="images/slide1.jpg" alt="" />');
    });
    

    【讨论】:

      【解决方案4】:

      这就是我们所做的,而且效果很好。我们跳过了设置imgsrc 属性并将img-location 添加到假属性lsrc。然后我们加载一个带有lsrc值的动态图片,并在加载后才设置实际图片的src

      这不是关于更快的加载,而是关于仅当图像完全下载到您的页面上时才显示图像,这样用户就不必看到令人讨厌的半加载图像。可以在加载实际图像时使用占位符图像。

      这是代码。

       $(function(){
          $.each(document.images, function(){
                     var this_image = this;
                     var src = $(this_image).attr('src') || '' ;
                     if(!src.length > 0){
                         //this_image.src = options.loading; // show loading
                         var lsrc = $(this_image).attr('lsrc') || '' ;
                         if(lsrc.length > 0){
                             var img = new Image();
                             img.src = lsrc;
                             $(img).load(function() {
                                 this_image.src = this.src;
                             });
                         }
                     }
                 });
        });
      

      编辑:诀窍是仅在临时 img 中加载该源时设置 src 属性。 $(img).load(fn); 处理这个问题。

      【讨论】:

      • 你怎么不就这么干了? $('img').each(function(){ $(this).attr('src', $(this).attr('lsrc')); });也许你的方式更快?
      • @DrewBaker 问题不是关于更快的加载,而是关于仅当图像完全下载到您的页面上时才显示图像,这样用户就不必看到那些烦人的半加载图像。
      • @simplyharsh 我确定我理解了。这意味着$(window).load(); 仍然需要等待图像加载完毕才能触发?
      • @FernandoSilva 不,这是诀窍的一部分。 Window-load 不会等待,因为它会在img 标签中寻找src 属性。 img 带有假 lsrc 属性的标签,对加载绑定没有任何意义。
      • @simplyharsh 我现在看到了,所以src 将是设置加载器 gif 的理想场所......谢谢
      【解决方案5】:

      最好的使用方式是b -lazy js。 bLazy 是一个轻量级的延迟加载图像脚本(压缩和压缩后不到 1.2KB)。它允许您延迟加载和多服务您的图像,因此您可以节省带宽和服务器请求。如果他/她不浏览整个页面,用户将有更快的加载时间并保存加载的数据。 有关选项、函数和示例的完整列表,请访问博客文章:http://dinbror.dk/blog/blazy

      以下示例是带有图像回调的延迟加载多服务响应式图像示例:) 如果您的设备宽度小于 420 像素,它将提供更轻更小的图像版本。加载图像后,它会在回调中删除加载器。

      在 HTML 中

       <img class="b-lazy"
           src="placeholder-image.jpg"
           data-src="image.jpg"
           data-src-small="small-image.jpg"
           alt="Image description" />
      

      在js中

      var bLazy = new Blazy({
              breakpoints: [{
              width: 420 // Max-width
                , src: 'data-src-small'
          }]
            , success: function(element){
              setTimeout(function(){
              // We want to remove the loader gif now.
              // First we find the parent container
              // then we remove the "loading" class which holds the loader image
              var parent = element.parentNode;
              parent.className = parent.className.replace(/\bloading\b/,'');
              }, 200);
              }
         });
      

      Example

      【讨论】:

        【解决方案6】:

        我正在使用以下内容来驱动我的滑块并提高页面加载性能。

           for (var i = document.images.length - 1; i >= 0; i--) {
              var this_image = document.images[i];
              var src = $(this_image).attr('src') || '' ;
              if(!src.length > 0){
                var lsrc = $(this_image).attr('lsrc') || '' ;
                if(lsrc.length > 0){
                  $(this_image).attr("src",lsrc);
                }
              }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-02
          • 1970-01-01
          相关资源
          最近更新 更多