等待图像加载,然后执行所有其他代码

发布于 2024-11-05 03:06:49 字数 1833 浏览 5 评论 0原文

好吧,我对此失去了理智。我确实在 SO 和谷歌上阅读了有关它的内容,我什至设置了预加载器(在 SO 上找到),但我发现的插件/代码都没有帮助我。

我想要做的是:等到所有图像都预加载,然后才执行所有其他 javascript 代码。就我而言,它可以(但不是必须)有“正在加载...”消息。

事实上,我在主体背景中有一个相当大的图像和另外两个也更大的图像,所以我想预加载它们,这样它们就会立即显示,并且不会有那种丑陋的“加载”图像效果。

这就是我现在正在使用的,但它不好:

$(document).ready(function()
{    
     preloadImages();
     ...some other code...

    function preloadImages(){
         imagePreloader([
            'images/1.png',
            'images/2.jpg',
            'images/3.png',                
         ]);
     } 

     function imagePreloader(arrayOfImages) {
         $(arrayOfImages).each(function(){
             (new Image()).src = this;
         });
     }   
}

我不知道,也许我应该在 .ready() 之外的某个地方调用这个预加载器?或类似的东西,请帮忙...

顺便说一句,是的,我也读过 这篇帖子,我不知道为什么,但 .ready() 对我来说工作得更快:(

编辑:
好吧,我终于让这个东西发挥作用了。我的问题?我把等待的div设置错了。这是我现在的代码:我有一个加载 div,它显示在所有内容之上,然后当所有图像加载时(使用 $(window).load(function(){...}); 正如我所建议的,隐藏该 div。

<div id="loading">
    <div id="waiting">
        <img class="loadingGif" src="images/loading.gif">            
    </div>
</div>

#loading
{
   background-size: 100%;
   background-color:#000;
   width: 100%;
   height: 100%;
   margin: 0px;
   padding: 0px;
   z-index:999;
}  

#waiting
{
   margin-left: auto;
   margin-right: auto;    
   position:absolute;
   top: 39%;
   left: 27.81%;
   width: 45%;
   height: 150px;    
   background-color: #FFF;
   border: 12px solid #FF771C;
   text-align: center;
}

并且我的 jQuery 代码是这样的:

$(window).load(function()
{    
    $('#loading').addClass('hidden');  
    ...
}

OK, I'm losing my mind over this. I did read here at SO and google about it, I even have the preloader set (found here on SO), but none of the plugins/code I found helped me.

What I want to do is: wait until all images are preloaded and only then execute all other javascript code. As far as I'm concerned it can (but not a must) have a "loading..." message.

The fact is that I have a pretty big image in the body background and 2 other images which are also bigger, and so I would like to preload them so that then they would show instantly and would not have that ugly "loading" image effect.

This is what I'm using now but it's not good:

$(document).ready(function()
{    
     preloadImages();
     ...some other code...

    function preloadImages(){
         imagePreloader([
            'images/1.png',
            'images/2.jpg',
            'images/3.png',                
         ]);
     } 

     function imagePreloader(arrayOfImages) {
         $(arrayOfImages).each(function(){
             (new Image()).src = this;
         });
     }   
}

I don't know, maybe I should call this preloader somewhere out of the .ready()? or sth like that, please help...

Btw, yes, I also read this post and I don't know why but .ready() works faster for me :(

EDIT:
Ok, so finally I got this thing to work. My problem? I was setting the waiting div wrong. This is my code now: I have the loading div which I show above everything and then when all images load (using $(window).load(function(){...}); as suggested I, hide that div.

<div id="loading">
    <div id="waiting">
        <img class="loadingGif" src="images/loading.gif">            
    </div>
</div>

#loading
{
   background-size: 100%;
   background-color:#000;
   width: 100%;
   height: 100%;
   margin: 0px;
   padding: 0px;
   z-index:999;
}  

#waiting
{
   margin-left: auto;
   margin-right: auto;    
   position:absolute;
   top: 39%;
   left: 27.81%;
   width: 45%;
   height: 150px;    
   background-color: #FFF;
   border: 12px solid #FF771C;
   text-align: center;
}

And my jQuery code is this:

$(window).load(function()
{    
    $('#loading').addClass('hidden');  
    ...
}

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

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

发布评论

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

评论(6

冷情妓 2024-11-12 03:06:49

您可以不尝试预加载,而只需在...上执行脚本,

window.onload = function(){..}

直到所有图像加载完毕后才会触发。

Instead of trying to preload, you could just execute your script on...

window.onload = function(){..}

That doesn't fire until all images have been loaded.

痴骨ら 2024-11-12 03:06:49

我有一个名为 waitForImages 的插件,可以让您在加载后代图像时附加回调。

就您而言,如果您想等待所有资源下载,$(window).load() 可能没问题。但你可以用我的插件做得更酷一点:)

var loading = $('#loading');

$('body').waitForImages(function()
{    
    loading.addClass('hidden');  
}, function(loaded, total)
{
    loading.html(loaded + ' of ' + total);

});

I have a plugin named waitForImages that lets you attach a callback when descendent images have loaded.

In your case, if you wanted to wait for all assets to download, $(window).load() may be fine. But you could do it a bit cooler with my plugin :)

var loading = $('#loading');

$('body').waitForImages(function()
{    
    loading.addClass('hidden');  
}, function(loaded, total)
{
    loading.html(loaded + ' of ' + total);

});
痴情换悲伤 2024-11-12 03:06:49

我认为您正在寻找的是 javascript:onLoad(),浏览器完成加载后就会调用它。

I think what you're looking for is javascript:onLoad(), which gets called as soon as the browser finishes loading.

呆萌少年 2024-11-12 03:06:49

您只能在加载图像后才显示图像,如下所示:

<img src="hdimg.jpg" width="1920" height="1080" style="display:none;" id="img1">

<script type="text/javascript">
$('#img1').load(function(){
    $(this).css({'display':'block'})
});
</script>

You could display the image only once it is loaded like so :

<img src="hdimg.jpg" width="1920" height="1080" style="display:none;" id="img1">

<script type="text/javascript">
$('#img1').load(function(){
    $(this).css({'display':'block'})
});
</script>
﹎☆浅夏丿初晴 2024-11-12 03:06:49

waitForImages 插件是一个有趣的插件,但解决方案可以通过以下方式实现:

    var counter = 0;
    var size = $('img').length;

    $("img").load(function() { // many or just one image(w) inside body or any other container
        counter += 1;
        counter === size && $('body').css('background-color', '#fffaaa');
    }).each(function() {
      this.complete && $(this).load();        
    });

The waitForImages pluggin is an interesting one, but the solution can be achieved just with:

    var counter = 0;
    var size = $('img').length;

    $("img").load(function() { // many or just one image(w) inside body or any other container
        counter += 1;
        counter === size && $('body').css('background-color', '#fffaaa');
    }).each(function() {
      this.complete && $(this).load();        
    });
ら栖息 2024-11-12 03:06:49

我相信最好的 jQuery 选项是使用 ajax get 调用:

$.get('image.png', {}, function(){
    // Do whatever you want here, this wont execute until image.png is preloaded
});

I believe the best jQuery option is to use the ajax get call:

$.get('image.png', {}, function(){
    // Do whatever you want here, this wont execute until image.png is preloaded
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文