jQuery、JavaScript、HTML:加载其他内容后如何加载图像?

发布于 2024-10-25 19:16:48 字数 884 浏览 1 评论 0原文

我有一个非常复杂的页面,有很多脚本和相当长的加载时间。在该页面的顶部,我想实现 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 个 1000x400px 的图像,这是相当大的。这些图像将在页面加载时加载。由于它们在我的标题中,这可能需要相当长的时间。

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

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

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

I have a very complex page with a lot of scripts and a rather long loading time. On top of that page I want to implement the jquery Nivo Slider (http://nivo.dev7studios.com/).

In the documentation it says I have to list all images for the slider inside of a 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>

However I might have 10 images with a 1000x400px which is quite big. Those images would load when the page loads. Since they are in my header this might take quite a while.

I looking for a way to use any jquery Slider Plugin (like the nivo slider) but either dynamically load images or load all those images after everything else on my page has loaded.

Any idea how I could solve that?

Is there even a way to start a javascript process after everything else on the page has loaded? If there is a way I might have an solution for my problem (using the jquery ajax load() method) ... However I have no idea how to wait for everything else to load and then start the slider with all the images.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

慕巷 2024-11-01 19:16:48

这就是我们所做的,而且效果很好。我们跳过了设置 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); 处理这个问题。

Here's what we did and its working great. We skipped setting src attribute of img and added img-location to a fake attribute lsrc. Then we load a dynamic image with lsrc value, and set the src of actual image only after its loaded.

Its not about faster loading, but its about showing the images only when its downloaded completely on your page, so that user do not have to see that annoying half-loaded images. A placeholder-image can be used while the actual images are being loaded.

Here's the code.

 $(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;
                       });
                   }
               }
           });
  });

Edit: Trick is to set the src attribute only when that source is loaded in temporary img. $(img).load(fn); handles that.

攒眉千度 2024-11-01 19:16:48

除了 Xhalent 的答案之外,还可以使用 jQuery 中的 .append() 函数将它们添加到 DOM 中:

您的 HTML 将具有:

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

然后您的 jquery 将是:

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

In addition to Xhalent's answer, use the .append() function in jQuery to add them to the DOM:

Your HTML would just have:

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

And then your jquery would be:

jQuery(function(){
    $("#slider").append('<img src="images/slide1.jpg" alt="" />');
});
感悟人生的甜 2024-11-01 19:16:48

查看 jquery load() 事件,它会等待所有内容,包括

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

加载图形,然后您可以加载图像使用:

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

您也可以向图像添加 onload 函数

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

check out jquery load() event, it waits for everything including graphics

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

on load you could then load the images using:

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

You can add a function onload to the image too

image.onload = function() {
  ...
}
遇到 2024-11-01 19:16:48

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

   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);
        }
      }
    }

I am using the below to power my slider and improve the page load performance.

   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);
        }
      }
    }
绮筵 2024-11-01 19:16:48

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

以下示例是带有图像回调的延迟加载多服务响应式图像示例:) 如果您的设备宽度小于 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);
        }
   });

示例

the best way to use is b -lazy js.
bLazy is a lightweight lazy loading image script (less than 1.2KB minified and gzipped). It lets you lazy load and multi-serve your images so you can save bandwidth and server requests. The user will have faster load times and save data loaded if he/she doesn't browse the whole page.
For a full list of options, functions and examples go to the blog post: http://dinbror.dk/blog/blazy.

The following example is a lazy loading multi-serving responsive images example with a image callback :) If your device width is smaller than 420 px it'll serve a lighter and smaller version of the image. When an image has loaded it removes the loader in the callback.

In Html

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

In 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

流云如水 2024-11-01 19:16:48

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

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

//your function implementation here...

});
</script>

jquery has a syntax for executing javascript after document has loaded:

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

//your function implementation here...

});
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文